If you have updated to Xcode 8 and when prompted to convert your source code to Swift 3, there are many migration issues: we examine some of them.
private
The existing code will need to rename private
to fileprivate
to achieve the same semantics. In many cases the new meaning of private
is likely to compile as well and the code will then run exactly as before.
const – let
In C, developers can control the mutability of these value types by using the const
keyword:
const NSPoint p = {1, 2}; p.x = 3; // Error: error: cannot assign to variable ‘p’ with const-qualified type ‘const NSPoint’
In Swift, developers control the mutability of these value types by using let
instead of var
:
let p = NSPoint(x: 1, y: 2) p.x = 3 // Error: cannot assign to property: ‘p’ is a ‘let’ constant
FIXME – functions added converting to current Swift syntax (edit menu)
// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l < r
case (nil, _?):
return true
default:
return false
}
}
// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l > r
default:
return rhs < lhs
}
}
toInt changed to Int
previous Swift versions
destination.nvert = (scene2Label.text?).toInt()
Swift 3.0.1
destination.nvert = Int(scene2Label.text!)
.floatValue changed to Float
previous Swift versions
// xarray[ivert!-1] = (lx.text as NSString).floatValue
Swift 3.0.1
xarray[ivert!-1] = Float(lx.text! as String)!
cancel “bool warning”
previous Swift versions
[lx.becomeFirstResponder()]
Swift 3.0.1
_ = [lx.becomeFirstResponder()]
A New Model for Collections and Indices
previous Swift versions
var i = c . index { $ 0 % 2 == 0 } let j = i . successor () print (c[j])
Swift 3.0.1
var i = c . index { $ 0 % 2 == 0 } // No change in algorithm API.
l e t j = c . i n d e x ( a f t e r : i ) / / A d v a n c i n g a n i n d e x r e q u i r e s a c o l l e c t i o n instance.
p r i n t ( c [ j ] ) / / N o c h a n g e i n s u b s c r i p t i n g .
for statement
previous Swift versions
for var i=1; i<nvert; i += 1 { // old style
Swift 3.0.1
for i in 1..<nvert {