Earlier this month, the third major iteration of Apple’s Swift programming language was released. The first developer preview has been bundled in with Xcode 8.0 beta 1, and there are a fair number of new features and improvements in the latest version of the language (which is still, of course, under active development). Swift 3.0 is also the first update to be launched after the language became open source in December 2015. In the following discussion, we will do a roundup of the best new features in Swift 3.0 for iOS app developers:
- Moving to Swift 3.0 – First things first, coders have to migrate to the new Swift platform. To facilitate this, Xcode 8 has been provided with a smart Migration Assistant, which manages most of the API name changes (in Cocoa) at one go. Certain changes, however, have to be made manually – since all the Cocoa APIs need to have separate names for Swift and Objective-C. A minor update – Swift 2.3 – has also been included in Xcode 8, to make the the migration process smoother. Swift 2.3 supports all the new Xcode features and software development kits (SDKs), and developers can use it to submit apps (instead of Swift 3.0) in the stable release of Xcode 8 as well.
- More compact coding – The open-source Swift started to ‘omit needless words’, and Swift 3.0 takes significant further steps in that direction. Words that are self-evident and self-explanatory no longer have to be written (for instance, ‘attributedString.appendAttributedString(String 2)’ becomes ‘attributedString.append(String 2)’ and ‘let red = UIcolor.redcolor()’ has to be written as ‘let red = UIcolor.red()’). As a result of this, word repetitions in strings (in particular) can be minimized.
- Swift on Linux and Windows – There were enough hints about Swift coming to platforms other than Apple as soon as the language became open-source. The speculations have been more or less confirmed by Craig Federighi at WWDC 2016. Coders should be able to port Swift 3.0 to Linux to start with, and Windows portability should be coming soon as well (Windows 10 already has Ubuntu Linux in the system). Swift was named in a StackOverflow survey as the ‘most loved language’, and app developers would certainly love the idea of it becoming portable to multiple platforms.
Note: The way in which Swift coding is used to make apps on macOS systems/Xcode will be different from how the language is used on Linux/Windows. Also, the chances of Google ditching Java in favour of Swift as the base language for making Android apps cannot be ruled out.
4. Revamped API design guidelines – In essence, three basic principle frame the new guidelines for API design in Swift 3.0. First, more weightage has to be given by iPhone app development experts on the clarity of their codes (instead of trying to make their codes as brief as possible). At the call site (or, the point of use) too, absolute code clarity has to be maintained. Finally, awareness about the various contextual cues is important, since they would have a vital influence on how the app codes are to be framed. The changes are pretty much easily evident when a code is run in a Playground with the Swift 3.0 migrator.
5. A swift-er Swift – Mobile app developers feel that the upcoming version of Swift will be faster than ever before. There are several solid reasons behind this belief – right from porting objects to the stack from the heap (a speed boost of nearly 25x) and string dictionary brushups (around 4x speed boost), to reduction in the volume of compiled code via Code Size Optimization (a whopping 75x speed boost). In Swift 3.0, multiple files can be cached by the compiler at a single time – and that enhances the performance factor as well. Swift was always a faster language than Objective-C, and with the latest update, the difference is going to increase quite a bit.
6. Mandatory labels for function parameters – Apple is constantly changing the naming convention of labels in methods in the Swift language. In Swift 2.2, those who make apps did not need to provide a label for the first parameter – because the method name already had the name built-in. That has been tweaked around in Swift 3.0, and now every single function parameter (including the first) need to have their own label names. Coders can simply change the last part of the method names to specify the corresponding labels. There is an option to work around this requirement as well. More power to Apple developers!
7. Binary interface stabilization – Another improvement coming to Swift 3.0 that hints at the availability of the language on other platforms. The upcoming version of Swift will be able to seamlessly interact with different operating systems – thanks to the much-improved and considerably more stable Binary interface (ABI). The interaction will take place at the binary level – and that, in turn, will make the portability of Swift 3.0 easier.
8. Lightweight Generics from Objective-C – The lightweight generics of Objective-C could be viewed in Swift 2.x and earlier versions – but the import process was complicated, and the final view was, at times, confusing. This issue has been addressed in Swift 3.0. To cite an example, an iOS application developer working with Swift would see [String], when
NSArray<NSString *> * is declared in Obj-C. The overall implementation of generics (via the libraries) will also be more complete in Swift 3.0. Mistakes while using NSLayoutAnchor can also be bypassed by developers who migrate to the latest Swift version.
9. Simpler handling of Core Graphics – Before Swift 3.0 rolled in, writing Core Graphics in Swift was a frustrating experience for many Apple software and app makers out there. Much to the delight of the developer fraternity, this is no longer the case – with the reworked C API making things a lot simpler. The Grand Central Dispatch (GCD) in Swift, which is mainly used for complicated threading tasks, has been overhauled as well. The process in which the UI can be freed up by using different threads for activities is rather straightforward in Swift 3.0.
10. Line control segments and Attribute arguments – The changes in these will probably be glossed over by many developers, and that would be a pity – for the new guidelines for line control segments and attribute arguments in Swift 3.0 are vital. The #sourceLocation(file:Line) syntax is required to be used in the line control statements. On the other hand, colons have to used in all attribute arguments in the latest iteration of Swift. Mistakes here will lead to errors being thrown, and detecting the same (particularly in a long piece of code) can be a tough ask.
11. C functions can be imported quickly – This is an excellent new feature for the library authors. The new attributes for C-functions in Swift 3.0 allow users to customize the way in which the functions will be imported. The functions now get directly mapped to the property method(s) of their corresponding objects. The new version of Swift does away with the need to call the same function/function name repeatedly, and performs method calls efficiently.The C-function mapping is accurate, time-saving and a handy tool for developers.
12. More focus on English – The latest iteration of the Swift language requires certain changes in how English language is to be used while coding. The imperative of verbs have to be used to name the mutating methods of operations, while for the non-mutating part, a suffix (‘ing’ or ‘ed’) is to be added. Whenever the latter is not possible (i.e., not grammatical), the verb has to be used in its present participle form. App makers, hence, need to be more careful while coding – and stay away from probable confusions while sorting arrays.
13. Arrival of #keyPath() – While writing long key-value observing (KVO) codes in Swift 2.x, there was always the chance of typos being made and remaining undetected. To make the task simpler, Apple has introduced the #keyPath() expression in Swift 3.0. Unlike in previous versions, the new expression allows static-type checking and supports autocomplete as well. A cool way to lower the chances of developer mistakes and resultant program crashes.
14. Properties/enums with lowerCamelCase – In Swift 2.2, certain parameters and properties had to be started with UpperCamelCase. These were more of exceptions, since in general, UpperCamelCase was meant for structs, enums and classes, while lowerCamelCase was to be used for parameters and properties. With Swift 3.0, things have become, thankfully, a lot more consistent. Both enums (which are like properties, anyway) and properties – without any exception – now have to be initiated with lowercase letters. Errors due to mistaken use of Upper/lowerCamelCase have little chance of cropping up.
Like the 2.2 update, Swift 3.0 comes with Apache License 2.0. While Objective-C programmers can migrate to the Swift platform with ease, the language is not going to become interoperable with C++ anytime soon. iPhone developers feel that quite a few other new features will arrive to Swift 3.0, between now and its final release. One thing is for sure though – the Swift language continues to evolve!