
Apple has just released it’s new and improved version of Swift. They have been listening to a lot of developer feedback and continue to make Swift better and easier to use. If you upgraded to the Xcode 6.3 and ran your project you’ll notice a lot of new errors, hopefully the details below will make it easier to upgrade your project.
What’s New in Swift 1.2
You’ll now notice that Xcode is really really fast again. Those of you who work on large scale projects will notice how fast it compiles now. In Xcode 6.2 our current project took 1:45 – 2:00 minutes to compile, making incremental changes infuriating, now it takes seconds to compile, saving so much time.
- Incremental builds — Source files that haven’t changed will no longer be re-compiled by default, which will significantly improve build times for most common cases. Larger structural changes to your code may still require multiple files to be rebuilt.
- Faster executables — Debug builds produce binaries that run considerably faster, and new optimizations deliver even better Release build performance.
Other than speed you’ll notice its a lot more reliable and easier to write code in Swift.
- Better compiler diagnostics — Clearer error and warning messages, along with new Fix-its, make it easier to write proper Swift 1.2 code.
- Stability improvements — The most common compiler crashes have been fixed. You should also see fewer SourceKit warnings within the Xcode editor.
Convert to the Latest Swift Syntax
The first step you may want to try is to let Xcode take care of as much as possible itself.
From the menu bar in Xcode > Edit > Convert > To Swift 1.2
A little instructions and warning that this will not fix all of the issues in your project for Swift 1.2.
Select your target and then it’s the same as how refactoring works – Xcode will work away and then come back with a preview of the code that needs to be changed.
You’ll see the old code and new code side-by-side with the changes needed.
Down Casting Improvements
as! for failable casts — Casts that can fail at runtime are now expressed with the new as! operator to make their potential for runtime failure clear to readers and maintainers of your code.
You’ll need to use the as! to convert between types, which used to work before.
var appVersion = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as String
Apple wants you to be more explicit with optional vs. non-optional type conversions
ViewController.swift:61:105: 'AnyObject?' is not convertible to 'String'; did you mean to use 'as!' to force downcast?
Use as! instead of as
var appVersion = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
let Constants
let constants are now more powerful and consistent — The new rule is that a let constant must be initialized before use (like a var), and that it may only be initialized, not reassigned or mutated after initialization.
Take enables patterns like this:
let x : SomeThing if condition { x = foo() } else { x = bar() } use(x)
This formerly required the use of a var even though there is no mutation taking place. Properties have been folded into this model to simplify their semantics in initializers as well.
if let Improvements
Prior to Swift 1.2 you could only optionally bind one at a time, now you can unwrap multiple optionals at once, as well as include intervening boolean conditions. This lets you express conditional control flow without lots of if – else nesting needed.
Here’s an example of if-let in a single statement:
if let language = swift?.language where language.type == kSwift, let objC = language.old? { doSomething() }
New Set Data Type
New native Set data structure — An unordered collection of unique elements that bridges with NSSet and provides value semantics like Array and Dictionary.
just like String, Array, and Dictionary are bridged to their corresponding Objective-C classes, the new Set data type is bridged to Objective-C’s NSSet class. Sets are generic collections so you need to provide the type of object to store; however, they store unique elements so you won’t see any duplicates.
var languages = Set() languages.insert("Objective-C") languages.insert("Swift") languages.insert("PHP") languages.insert("Perl") if languages.isEmpty { println("I don't know any computer languages.") } else { if languages.contains("Objective-C") || languages.contains("Swift") { println("I know how to make iOS apps.") } }