Apple released Swift 3.0 at the 2016 Worldwide Developers Conference (WWDC16). Swift 3.0 is a huge release containing major improvements and refinements to the core language and Standard Library, major additions to the Linux port of Swift, and the first official release of the Swift Package Manager. The following will focus on what’s new in Swift 3.0, from Swift compiler and standard library to illustrate changes.
- Function or method parameters
a. The first argument must be specified parameter name when to call the function or method. Unless use “_” clearly stated that the omission parameters
b. func add(var a : Int){
a += 1
}// this code will be wrong, should change as below code
func add(a : int){
var a = a;
a += 1
}
- The return value of function
In Swift 3.0, the return value of function must be received, otherwise, will report warning message. The main purpose is to avoid developer forget to receive return values, but in some cases, doesn’t use the return value, we can use “_” to ignore the return value. Also, we can add @discardableResult statement, tell compiler that this method can not receive return value
- Optional type
Optional type control is more rigorous in Swift3.0
let a : Int! = 1
let b = a + 1// Forced to unpack, b is the Int type
let c = a // c is Int? but in old swift is Int!
- The change for Selector
class MyClass {
@objc func add(a:Int,b:Int) -> Int {
return a + b
}
func func01() {
let _ = #selector(sum(a:b:))
}
}
- The optional method in protocol
If you want to define the optional method in old Swift, only need to add @objc in protocol but in swift3.0 , need to add @objc in protocol ,also need to add @objc in optional method
@objc protocol MyProtocol {
@objc optional func func01()
func func02()
}
- For Loop
for var i = 0; I < 10; i += 1 {
debugMethod(i)
}// the above code is wrong in swift3.0
for i in 0 .. < 10 {
debugMethod(i)
}
Also swift 3.0 contains many changes if you interested in swift3.0 you could visit below URL:
https://github.com/apple/swift-evolution/tree/master/proposals