In October, Apple released Swift 2.1 – the latest stable version of the programming language that is fast gaining in popularity among iOS app developers worldwide. Swift 2 builds on the original version of the language, and offers a fair number of additional features and functionalities to aid programmers working with it. The second beta release of Xcode 7.1 has Swift 2.1 bundled with it, and early reviews have been – expectedly – positive. Let us here elaborate a bit on the new features that Swift 2 comes with:
- Protocol Orientation – Unlike what was the case with Swift 1, protocols are no longer like interfaces in the new version of the language. Instead, those coding for iOS apps can now include default implementations for calling methods and properties. The ‘protocol extension’ feature of Swift 2 allows this – and it makes the function chains inside a program a whole lot more compact and scannable. Nearly all ‘Foundation’ objects are compatible with the new ‘CustomStringConvertible’ protocol (which has replaced ‘Printable’). Developers can now either apply a general implementation across types, or extend their programs with custom code snippets.
- Targeting the ‘right’ OS versions – Every new version of the iOS SDK comes with a host of new APIs. At times, it becomes a problem for software and mobile app developers to write out code that would be properly executable on the OS version(s) for which an application is being built (compatibility with older OS versions was an issue, in particular). Swift 2 makes this all too easy – thanks to the error notifications generated by its compiler whenever certain APIs or other resource is not compatible with the OS version a developer is targeting. In essence, this makes creating apps for the relatively older iOS versions safer, simpler, and error-free. The #available block is a big help for coders while targeting specific OS versions.
- Using the ‘guard’ – ‘guard’ is a new keyword that comes in handy when iPhone programmers are putting conditional checks at the start of methods in their programs. At first glance, using the ‘guard’ keyword might seem rather similar to writing the ‘if’ statement – but a closer look would highlight the differences. For one thing, using the new keyword ensures that whenever the conditions are not fulfilled, the code stops executing. Also, it can be used to unwrap optionals – with the advantage that the optionals can be reused in any other section of the code block (a departure from the ‘if’ statement here). iOS app development experts also agree that the ‘guard’ keyword has a role in making the overall program shorter and more concise.
- Try-Throw-Catch for error handling – Swift 1 (and its iterations) had a lot going for it – but the language had one major shortcoming. There was no reliable error handling method in it, and software/app developers had to go the whole hog of passing methods that are likely to generate errors with the NSError object. Swift 2, on the other hand, has a stable exception-based model in place for error handling purposes. At the start of the method call, a ‘try’ keyword needs to be added, while errors/bugs are actually identified with the ‘do-catch’ statement. In a nutshell, whenever a throwing method is invoked, programmers only have to put the ‘try’ keyword before it. Simple enough, right?
- Playgrounds get smarter – Features like Quick Look and Timeline Assistant (the latter is particularly useful for previewing SpriteKit animations and other complex UI views) make working with ‘Playgrounds’ an absolute breeze with Swift 2. Since results of code lines are viewable real-time, debugging and mobile app testing become quicker and easier too. Xcode 7 also supports usage of rich text in comments, along with links and images. From lists to graphics (including values) – the revamped Playgrounds add a nice interactive feel to coding with Swift 2.
- Swift 2 is set to become open-source – ‘By the end of 2015’ was the deadline that Apple gave at this year’s WWDC, for making the updated version of Swift open-source. Some weeks back, Chris Lattner, the creator of the LLVM project, confirmed this. The open-source version of Swift will have comprehensive Linux support, along with the built-in standard library and the code safety options. The prospect of working with Swift 2 on multiple platforms is something every software and mobile app development expert is looking forward to. It remains to be seen when the big news actually arrives.
- Swift 2 is more swift – The initial version of Swift was, on average, about three times faster than Objective-C – and Swift 2 is makes things a lot, well, swift-er. Programmers no longer have to use optionals frequently, since the annotated APIs can no longer return the ‘null’ value. In addition to this ‘nullability annotation’ feature in Objective-C, the language also has a new generics system that ensures that detailed information about codes written in Swift 2 can be preserved with ease. As mentioned many times already, Swift has access to all Objective-C APIs, and Apple is endeavoring to make the two languages more interoperable.
- Viewing headers in Swift 2 – The absence of headers in Swift posed one significant challenge for programmers and iPhone app developers. In a lengthy code, there was no provision of viewing all the functions that had been used – except for, of course, manually parsing through the entire code. Swift 2 in Xcode 7 does away with this issue – thanks to the synthesized header files that are generated automatically. This method quickly goes through the entire program and generates virtual header files – to help coders view all the exposed functions and methods. Now, it’s about getting the best of both worlds – no need to update header files (everything is stored in the .swift file), and the option to inspect function calls with synthesized headers.
- Println() gives way to Print() – Yet another developer-friendly feature of Swift 2 is the new ‘print()’ method – which has replaced the ‘println()’ of Swift 1. In addition to using the method to writing stuff in the output view, outputs can now be generated with a newline as well. For that, iOS developers have to tweak the value of the ‘appendNewline’ parameter to ‘true’. The functionalities of println() and print() have been combined together in Swift 2.
- Migrating to Swift 2 – Moving up from Swift 1 (or Swift 1.2, for that matter) to Swift 2 is fairly simple for Apple developers. The new version of the language has an in-built Swift 1-to-2 migrator – which allows coders to keep their programs updated with the latest standards and the new set of syntaxes. With the help of the migrator, developers can even access the new error-handling methods that are a major highlight of the Swift 2 language.
- Real-time mutability warnings – Errors happen while coding for software and mobile apps. One such common error is declaring something as a ‘variable’ when it should ideally be declared as a ‘constant’ (people working with Swift prefer ‘constants’ over ‘variables’ anyway). In Swift 2, whenever a variable that cannot be changed is declared, a ‘mutability warning’ is generated. The Xcode 7 IDE can actually ‘understand’ how variables are declared and whether they need to be changed.
- Using the ‘defer’ – Another new keyword that is instrumental in cleaning up Swift codes and making them more readable and customized. Just as ‘guard’ prevents code from executing when condition(s) are not met, ‘defer’ allows a code block to be executed, even in the presence of errors. This execution takes place before the method ends (i.e., the code included in the ‘finally’ clause). In practice, as soon as error messages are thrown by the ‘doStuff‘ function, the method exits and the ‘defer’ code is invoked.
- Improvements in code syntaxes – We have already talked about the ‘guard’ and ‘defer’ keywords in detail. Swift 2 brings in lots of other syntax improvements as well – right from extended pattern matching (for ‘for’ loops and ‘if’ clauses), to the unified keyword naming standards. The ‘repeat-while’ loop has replaced the old ‘do-while’ loop. The ‘repeat’ keyword also has a role to play in streamlining the control flow of codes written in the new version of Apple’s language.
- Whole Module Optimization in Swift 2 – For detailed analysis of all the source code included in a module, Xcode 7 has a new ‘Whole Module Optimization’ feature for Swift 2 users. This additional optimization level not only reduces chances of errors remaining undetected, but also boosts up code compilation speeds. iOS developers have also received the option of writing Markdown inside comments. The focus is clearly on making Swift more usable than before.
- if-case to support Switch statements – Swift developers are already familiar with the ‘switch’ statements that can be used for pattern and range matchings. In the newly released version, ‘if case’ can also be used (in addition to ‘switch’) for both the types of matchings. Temporary bindings can be created in ‘if-case’ too, just like in switch statements. Using ‘if-case’ in program tuples is pretty much straightforward.
In Swift 2, enums can now be directly printed on the console. The consensus among iOS app developers is that the new language goes a long way in improving the existing Cocoa frameworks as well as the coding methods for software and mobile applications. Craig Federighi had said that Apple was ‘stepping on the gas this year with Swift 2’ – and the statement nicely sums up the range of new features that the programming language now has.
Latest posts by Hussain Fakhruddin (see all)
- The Rise & Rise Of The Popularity Of React Native - April 18, 2019
- The Rise Of Empathetic Technology: All You Need To Know - April 15, 2019
- Getting Acquainted With Flutter: A Beginner’s Guide - April 12, 2019