Skip to main content

Development

What’s New in Swift 3.0?

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.

  1.  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

}

  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

  1. 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!

  1. The change for Selector

class MyClass {

@objc func add(a:Int,b:Int) -> Int {

return a + b

}

func func01() {

let _ = #selector(sum(a:b:))

}

}

  1. 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()

}

  1. 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

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories
Follow Us