Xcode 8.0 comes with a Swift Migrator tool that helps you migrate your project to Swift 3, or update it to work with Swift 2.3 and the new SDKs.
Swift Migration Assistant
When you open your project with Xcode 8.0 for the first time, you will be prompted via the migration assistant to do a migration pass. The assistant can also be invoked manually from the menu Edit -> Convert -> To Current Swift Syntax…
You can choose from two kinds of migration to perform:
Use Swift 2.3 Modifies your project to enable the Use Legacy Swift build setting and provides source changes to be able to build against the new SDKs.
Use Swift 3 This is recommended. You will get source changes to be able to build your project using Swift 3 and take advantage of all the new features in Xcode 8.0.
Optionally, you can move to Swift 2.3 now and invoke the migration assistant again later to update to Swift 3.
Refined API Naming in Swift 3
I found following changes when I was migrating my code to Swift 2.2 to Swift 3.
- Instantiate ViewController With Identifier
1234//Swift 2.2let aController = self.storyboard.instantiateViewControllerWithIdentifier("identifier")//Swift 3let aController = self.storyboard.instantiateViewController(withIdentifier: "identifier") - User Defaults
12345678910111213141516//Swift 2.2let userDefaults = NSUserDefaults.standardUserDefaults()if let isYourValue = userDefaults.objectForKey("key") {}else {userDefaults.setBool(false, forKey: "key")userDefaults.synchronize()}//Swift 3let userDefaults = UserDefaults.standardif let isYourValue = userDefaults.object(forKey: "key") {}else {userDefaults.set(false, forKey: "key")userDefaults.synchronize()} - Open URL
1234//Swift 2.2UIApplication.sharedApplication().openURL(NSURL(string: "tel://919714494770")!)//Swift 3UIApplication.shared.openURL(URL(string: "tel://919714494770")!) - Perform Segue with Identifier
1234//Swift 2.2performSegueWithIdentifier("toView", sender: nil)//Swift 3performSegue(withIdentifier: "toView", sender: nil) - Dequeue Reusable Cell With Identifier
1234//Swift 2.2let cell = tableView.dequeueReusableCellWithIdentifier("Cell")!//Swift 3let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! - Cell for row at IndexPath
1234//Swift 2.2func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {//Swift 3func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell { - UIApplication Shared Application
1234//Swift 2.2UIApplication.sharedApplication().keyWindow!//Swift 3UIApplication.shared.keyWindow! - String Encoding Unicode
1234//Swift 2.2NSUnicodeStringEncoding//Swift 3String.Encoding.unicode - String Encoding UTF8
1234//Swift 2.2NSUTF8StringEncoding//Swift 3String.Encoding.utf8 - UIButton set title with state
1234//Swift 2.2UIButton -> setAttributedTitle(str, forState: self.state)//Swift 3UIButton -> setAttributedTitle(str, for: self.state) - CGSize
1234//Swift 2.2let size = CGSizeMake(CGRectGetWidth(self.bounds), CGFloat(MAXFLOAT))//Swift 3let size = CGSize(width: self.bounds.width, height: CGFloat(MAXFLOAT)) - CGPoint
1234//Swift 2.2CGPointMake(0, 50)//Swift 3CGPoint(x: 0, y: 50) - Matches in NSRegularExpression
123456//Swift 2.2let regEx = try NSRegularExpression(pattern: text, options: [])ranges = regex.matchesInString(self.string, options: [], range: NSMakeRange(0, self.string.characters.count)).map {$0.range}//Swift 3let regEx = try NSRegularExpression(pattern: text, options: [])ranges = regex.matches(in: self.string, options: [], range: NSMakeRange(0, self.string.characters.count)).map {$0.range} - Pop to root view controller
1234//Swift 2.2navigationController.popToRootViewControllerAnimated(true)//Swift 3navigationController.popToRootViewController(animated: true) - Hide and show any control
1234//Swift 2.2self.viewSample.hidden = true//Swift 3self.viewSample.isHidden = true - NSTimer to Timer
123456//Swift 2.2var timer : NSTimer!timer = NSTimer.scheduledTimerWithTimeInterval(Double(1.0), target: self, selector: #selector(self.yourMethod), userInfo: nil, repeats: true)//Swift 3var timer : Timer!timer = Timer.scheduledTimer(timeInterval: Double(1.0), target: self, selector: #selector(self.yourMethod), userInfo: nil, repeats: true) - CGPointZero
1234//Swift 2.2CGPointZero//Swift 3CGPoint.zero - Prepare for Segue
123456//Swift 2.2override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {}//Swift 3override func prepare(for segue: UIStoryboardSegue, sender: Any?) {} - Dispatch Queue
123456//Swift 2.2dispatch_async(dispatch_get_main_queue()) {}//Swift 3DispatchQueue.main.async {} - UIColor & CGColor
1234//Swift 2.2UIColor.blackColor().CGColor//Swift 3UIColor.black.cgColor - CGSize
1234//Swift 2.2CGSizeMake(0, 5)//Swift 3CGSize(width: 0, height: 5) - Select and deselect any control
1234//Swift 2.2UIButton.selected//Swift 3UIButton.isSelected
And more…
Happy Coding 🙂