“Use struct to create a structure. Structures support many of the same behaviors as classes, including methods and initializers.One of the most important differences between structures and classes is that structures are always copied when they are passed around in your code, but classes are passed by reference.”
摘錄來自: Apple Inc. “The Swift Programming Language (Swift 3)”航唆。 iBooks.
? 在swift中struct與class具有很多相同點,但其中一個區(qū)別是class是引用观腊,struct是copy幻梯。
? 繼承允許一個類繼承另一個類的特征
? 類型轉(zhuǎn)換允許在運行時檢查和解釋一個類實例的類型
? 解構(gòu)器允許一個類實例釋放任何其所被分配的資源
? 引用計數(shù)允許對一個類的多次引用
mutaing與extension
“Notice the use of the mutating keyword in the declaration of
SimpleStructure to mark a method that modifies the structure. The declaration of SimpleClass doesn’t need any of its methods marked as mutating because methods on a class can always modify the class”
注意聲明 SimpleStructure 時候 mutating 關(guān)鍵字用來標(biāo)記一個會修改結(jié)構(gòu)體的方法。 SimpleClass 的聲明不需要標(biāo)記任何方法,因為類中的方法通吵适唬可以修改類屬性(類的性質(zhì))
“Use extension to add functionality to an existing type, such as new methods and computed properties. You can use an extension to add protocol conformance to a type that is declared elsewhere, or even to a type that you imported from a library or framework”
使用 extension 來為現(xiàn)有的類型添加功能,比如新的方法和計算屬性拷泽。你可以使用擴展在別處修改定義,甚至是從外部庫或者框架引入的一個類型,使得這個類型遵循某個協(xié)議
摘錄來自: Apple Inc. “The Swift Programming Language (Swift 3)”。 iBooks P43.
defer
“Use defer to write a block of code that is executed after all other code in the function, just before the function returns. The code is executed regardless of whether the function throws an error. You can use defer to write setup and cleanup code next to each other, even though they need to be executed at different times”
defer是swift2.0推出的語法袖瞻,用于在code最后執(zhí)行一些清理工作司致,如文件讀取操作,在最后關(guān)閉文件聋迎,就可以放在defer中脂矫。
摘錄來自: Apple Inc. “The Swift Programming Language (Swift 3)”。 iBooks P48.
整形浮點型轉(zhuǎn)化
“l(fā)et three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Double(three) + pointOneFourOneFiveNine
// pi equals 3.14159, and is inferred to be of type Double”
注意類型轉(zhuǎn)換的方法是Double(three)而不是(Double)three,我的理解是有一個內(nèi)置構(gòu)造器用來生成一個新值砌庄。
摘錄來自: Apple Inc. “The Swift Programming Language (Swift 3)”羹唠。 iBooks P76.