Difficulties converting to Swift 3
Solution 1
Most of them are easy fixes, simply by tapping the red button, and having Xcode fix it for you! Others include:
CGRect
Swift 2:
let frame = CGRectMake(0, 0, 20, 20)
Swift 3:
let frame = CGRect(x: 0, y: 0, width: 20, height: 20)
CGPoint
Swift 2:
let point = CGPointMake(0, 0)
Swift 3:
let point = CGPoint(x: 0, y: 0)
CGSize
Swift 2:
let size = CGSizeMake(20, 20)
Swift 3:
let size = CGSize(width: 20, height: 20)
CGRectGetMidX
Swift 2:
CGRectGetMidX(view)
Swift 3:
view.midX
CGRectGetMidY
Swift 2:
CGRectGetMidY(view)
Swift 3:
view.midY
UIColor
Swift 2:
let color = UIColor.redColor()
Swift 3:
let color = UIColor.red
"NS"
Swift 2:
NSTimer
NSData
NSError
Swift 3:
Timer
Data
Error
UserDefaults
Swift 2:
NSUserDefaults.standardUserDefaults().//something
Swift 3:
UserDefaults.standard.//something
Solution 2
And always remember to use the helpful "Fix all in Scope" function which can be found at Editor -> Fix all in Scope
Solution 3
I was converting a project and Xcode was not helping me with any fixes so I resorted to a couple of regex search-and-replaces:-
CGPointMake\((.*),[ ]*([^\)]+)\)
CGPoint(x:$1, y:$2)
CGSizeMake\((.*),[ ]*([^\)]+)\)
CGSize(width:$1, height:$2)
Note they are not aware of nested parentheses , but probably good enough for 90% of cases.
Related videos on Youtube
niravdesai21
Updated on March 14, 2020Comments
-
niravdesai21 about 4 years
After converting from Swift 2 to Swift 3 (even after converting edit-> convert -> to current swift syntax) I am getting lots of errors. Especially:
I am shown total 90 errors for my project which was working fine in Swift 2 before i downloaded this beta Xcode 8.0 and converted to Swift 3
Is this a conversion mistake I am making?
-
niravdesai21 almost 8 years@EricD i want to know is the method to convert code i use edit-> convert -> to current swift syntax is correct or not. If there is a better method then just to help me with that. thats all i asked for.
-
Eric Aya almost 8 years@niravdesai21 Yes this is the right command. This command does most of the work, but it's up to you to do the rest. Xcode is just an IDE, not a magical thing. :) Read the Swift 3 docs if you're lost with the changes.
-
Pranav Wadhwa over 7 yearsAlso, it is possible that the conversion didn't work right. Try converting it again, even if it gives you a warning that the file was already converted to Swift 3
-
DG7 over 6 yearsI had over a 100 errors when I converted. Come to find about 80% of the errors were resolved when I updated my libraries. In particular I was using Swifty JSON. I uploaded the latest file version and voilà, the rest was manageable stepping through code.
-
-
Tyler Hack over 7 yearsThis helped me out a lot, while I was working I found one more that was a recurring change CGRectGetMidX && CGRectGetMidY Swift 2: CGRectGetMidX(object) || CGRectGetMidY(object) Swift 3: object.midX || object.midY
-
Pranav Wadhwa over 7 yearsThanks @TylerHack I'll include that in my answer. Glad to help!
-
nights about 7 yearsi have hundreds of CGRectMake etc, how is that easy to change? maybe by regexp, but even so quite dangerous
-
Pranav Wadhwa about 7 years@nights try converting it again. I believe the default conversion will fix it.
-
nights about 7 yearstried a hundred times and it will not convert anything :[ now making changes manually...counting days
-
Vitya Shurapov almost 7 yearsAlso size, point and rect with 0 as the values, it can be done like this.
-
Vitya Shurapov almost 7 yearslet size = CGSize.zero, let point = CGPoint.zero, let rect = CGRect.zero