前段時(shí)間,做了一些筆記,都是很基礎(chǔ)的,接下來準(zhǔn)備用幾天的時(shí)間來做個(gè)小的總結(jié)整理.
- Question 1: Swift中何時(shí)用?何時(shí)用!
Answer: 一般?聲明的為可選類型,可以先讀讀文檔Optional Chaining來理解下.簡單來講就是,如果一個(gè)對(duì)象的類型為String幽钢?那么它就有兩種可能:一種是沒有設(shè)置的,即nil,另一種就是String的值.而當(dāng)該對(duì)象用!來取值時(shí),表示確信其一定會(huì)有String值,此時(shí)值若為nil,程序會(huì)報(bào)錯(cuò)崩潰.文章Swift之?和!里面有該問題的深入剖析.另外CS193p 57分鐘以后也會(huì)有關(guān)于此問題的簡單講解.其中有幾句話能夠便于理解 when it is not set,it's nil. Or something.
String? So we would call this an optional String,But it's really an optional,it's not a String that can be nil,it's an optional that can be a String
- Question 2: 什么時(shí)候用Class,什么時(shí)候用Struct样眠?
Answer: 搞清楚這個(gè)問題之前,有必要復(fù)習(xí)下基礎(chǔ)知識(shí)Classes and Structures
從文檔這句話structure instances are always passed by value, and class instances are always passed by reference.可以看出其最大的區(qū)別在于structure為值傳遞而class為引用傳遞.這個(gè)問題的回答中有個(gè)例子,可以解釋這其中的區(qū)別.而文章When to Use Swift Structs and Classes中指出use structs when you need value semantics, and use classes when you need reference semantics. 也可以看看中文譯文.
- Question 3: "Call can throw,but it is not marked with ’try’ and the error is not handled"是什么錯(cuò)誤?
Answer: 老項(xiàng)目升級(jí)到Swift2.0時(shí),遇到了這個(gè)問題.從提示中可以看出,我們沒有做Error Handle.這是蘋果為提高Swift的安全性在Swift2.0所加的新特性.可以看看下面的文章來學(xué)習(xí)下:
- Error handling in Swift 2: try, catch, do and throw
- Swift 2.0 Error Handling
- Error Handling in Swift 2.0
最后來看集WWDC吧:) What's New in Swift
Girl學(xué)iOS100天 第21天