swift 3.0 新特徵

限定做用域訪問級別:

在swift 3.0 以前,swift 提供3種不一樣的訪問級別:
1.public(公開)
2.internal(內部)
3.private(私用)
默認的訪問級別是internal,這意味着此成員只能在模組(module)內可見。若是要讓其可以被模組外的成員訪問,那麼就要將其設置成public。此外就是private,這意味着私有成員可以在文件內部可見。函數

在swift 3.0當中,咱們將迎來另一種訪問級別,private 將會被從新命名爲 fileprivate :即文件的私有成員只可以在文件內部可見;咱們將獲得第四種訪問級別就是 private ,這也就說即便在同一個文件當中,私有成員也只可以在對應的做用域當中可見。所以,swift 3.0中,最終的訪問級別就是:
1.public(公開)
2.internal(內部)
3.fileprivate(文件內私有)
4.private(私有)code

移除 ++ 和 --

自增運算符 ++ 與自減運算符 -- 將被移除,取而代之的即是 count+=1 與 count-=1
例如:ci

while count < 10{
    count += 1
}

while num < 10{
    num -= 1
}

將inout 聲明調整爲類型修飾

參數是一種不可修改的拷貝,若是你想修改你的傳入參數,並禁止拷貝的,那麼您能夠將其聲明爲 inout .
在swift 2.2中:作用域

func myfunction(inout input:Int){

}

而在swift 3.0中,將調整爲類型修飾,如;input

func myfunction(input : inout Int){

}

inout 限制爲只能獲取 @noescape 的上下文

inout的另外一個改變,是其的捕獲機制受到限制。編譯器

func escape(f:()->()){}

func example(x :inout Int){
    escape{ _ = x }
}

一個名爲 escape()的函數,它接受一個方法做爲其參數。在 example() 方法當中,咱們引入 inout 類型的 x 的參數,並將其傳遞到 escape方法內。it

當 escape() 函數對 inout x 進行操做時,會形成一個問題,因爲 inout 的開銷很大。inout 會對傳入的變量進行修改 ,而這個時候,並不肯定 example()函數是否可以調用其做用域以外的函數。io

爲了解決這個問題,咱們可使用 @noecape 來標記:編譯

func escape( f:@noecape()->()){

}

//swift 3.0以前的寫法:
func escape(@noescape f:()->()){

}

func exmaple(x:inout Int){
   escape{ _ = x}
}

這也就意味着,我將告訴編譯器,我傳遞的這個函數 不會使用任何做用域範圍以外的東西,所以程序可以正常運行。

將 @noescape 和 @autoclosure 轉化成類型特質

func escape(f: @noescape ()->()){

}

func noEscape(f :@autoclosure ()->()){

}

將用於關聯類型聲明的 typealias 替換成 associatedtype

protocol Prot{
    associatedtype Container : SequenceType
}

這表示您須要明確告知Container 隨後會關注哪一種類型

extension prot{
    typealias Element = Container.Generator.Element
}

這種用法與 #define 相似

相關文章
相關標籤/搜索