WWDC 2016 Spotlight: Swift 3

David Brown
BPXL Craft
Published in
4 min readJun 17, 2016

--

Swift was quickly adopted by our developers and many others upon its launch in 2014. But, with Swift’s first major update announced at WWDC 2016, Apple can expect its programming language to have greater use and optimization from the community than ever before.

Apple released Swift as an open-source project last December. Since then, developers have worked diligently to make the language seamless. Additionally, Apple’s internal teams have been using Swift in more of its own apps, including the new Music app, Console app, and Agents and Daemons picture-in-picture feature. The new documentation viewer in Xcode 8 as well as the new Swift Playgrounds for iOS are completely written in Swift.

Swift 3 is a product of the hard work from developers at Apple and those within the widespread community of app creators. Black Pixel developer Kai Schaller and I would like to share some initial thoughts about the recent changes to the language and how these updates will impact developers.

Imposing Opinions on Libraries

Up until Swift 3, it felt like Swift’s main concerns were developing an opinion and maintaining interoperability with a legacy Objective-C codebase. Swift 3 is the release that says, “We think we found our voice.” Swift 3 is when Swift starts to impose its opinions on the existing libraries, and among the manifestations is the grand renaming of system-supplied classes, dropping the NS prefix we’ve known for decades. This change goes much further, as many C APIs that we’ve lived with for decades will be getting object-oriented makeovers, as detailed in the “Swift API Design Guidelines” WWDC session.

Hitches in Code Migration

Since Swift 3 is a non-source compatible change, Apple is providing migration helpers in Xcode. Running this migration tool on some existing Swift code shows that the conversion is mostly painless but not complete without changing some class references and fixing some method calls that were no longer valid. Of course, it is important to mention that these issues are with the very first beta of the migrator and will likely be fixed or changed as the tool gets closer to release. Some non-comprehensive examples of code that were not translated automatically include:

  • Swift 3 wants enum values to begin with a lowercase letter and generally fixes that automatically. However, if you refer to the enum value in a fully specified manner, like “DataError.Unknown,” it will not lowercase the first letter of the value (“u” in this case).
  • NSMutableURLRequest doesn’t exist anymore. It has been replaced with “URLRequest” and is now a struct rather than a class. Any references to NSMutableURLRequest that exist need to be manually changed. In addition, because URLRequest is a struct, it needs to be stored in a variable (var) rather than a constant (let) if any methods are called that modify the request.
  • It is now a warning if a method returns a value and is not assigned to anything, even if it is not an error to ignore the return value. To silence the warning, the result of the method call needs to be manually assigned to “_”.

Anomaly in the Conversion Process

Despite the new convention to reference all parameters by name by default (as opposed to automatically omitting the first parameter name), the Swift 3 conversion process will add “_” to the first parameter of the function instead of adding the parameter name in the code that calls the function.

Community-Driven Updates

Overall this is an evolutionary change, compared to the revolutionary change brought forth when Swift was initially introduced. This is not to imply that this is an insignificant release. Swift 3 can be considered the first release that is largely community driven. All changes, regardless of whether they come from within or outside of Apple, go through the same transparent proposal and review process, including in-depth discussions in which anyone can participate.

If you’re ready to dive into Swift, check out Swift.org where you can find all of the tools and resources you need as well as contribute ideas and expertise to the project. Let us know on Twitter about what you’re most excited for with the updates available in Swift 3. And stay tuned as we dive deeper into the improvements to Swift’s developer tools and debugging as well as the creative world of Swift Playgrounds.

A non-comprehensive list of sessions that led to this article:

For more insights on design and development, subscribe to BPXL Craft and follow Black Pixel on Twitter.

--

--