Tips for Upgrading to Swift 1.2 and Xcode 6.3

Become a Subscriber

Now that Swift 1.2 is out of beta and Xcode has been updated to 6.3, it’s time to upgrade your apps to the latest Swift syntax!

First of all, you need to know about the Swift Syntax wizard provided by Xcode. Using this wizard will do the majority of the work for you:

  1. Go to the Edit menu
  2. Choose Convert
  3. Select “To Latest Swift Syntax”

Make sure to read through the latest API changes in 8.3 as there are many large differences in some of the SDK and you will most likely be impacted by them. You can save yourself a lot of time by being proactive rather than reactive in learning about the updates directly from the source.

Common errors

  • Rename countElements() to just count()
  • Forced downcasts us as! instead of as
  • Use Set<Type> instead of NSSet
  • Change "string".utf16Count to count("string".utf16)

Updated Method Signatures

// Before 1.2
touchesBegan(touches: NSSet, withEvent event: UIEvent)
touchesEnded(touches: NSSet, withEvent event: UIEvent)
touchesMoved(touches: NSSet, withEvent event: UIEvent)

// After 1.2
touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent)
touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent)

Class Updates

UIViewController no longer has a required, parameterless initializer. Thus if you previously used it in a subclass, you will have to update:

// Before Swift 1.2
public class Modal: UIViewController
  required override public init()

// After 1.2
public class Modal: UIViewController
  override public init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)
    super.init(nibName:nibNameOrNil, bundle:nibBundleOrNil)