swift學習筆記0

開始學swift,隨便記錄一下。(英文部分摘自官方文檔)

1 optionals 可選

You use optionals in situations where a value may be absent. An optional represents two possibilities: Either there is a value, and you can unwrap the optional to access that value, or there isn’t a value at all.

如果一個值可能是空的,就可以使用optionals。optional 表示有兩種可能性:要麼存在一個值(你可以獲取這個值),要麼值根本不存在。
看個官方的例子:

Here’s an example of how optionals can be used to cope with the absence of a value. Swift’s Int type has an initializer which tries to convert a String value into an Int value. However, not every string can be converted into an integer. The string “123” can be converted into the numeric value 123, but the string “hello, world” does not have an obvious numeric value to convert to.

The example below uses the initializer to try to convert a String into an Int:

let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
// convertedNumber is inferred to be of type "Int?", or "optional Int"

Because the initializer might fail, it returns an optional Int, rather than an Int. An optional Int is written as Int?, not Int. The question mark indicates that the value it contains is optional, meaning that it might contain some Int value, or it might contain no value at all. (It can’t contain anything else, such as a Bool value or a String value. It’s either an Int, or it’s nothing at all.)

在swift中,Int類型有一個初始方法,把string類型的值轉換爲Int值。但是,並不是每個string都能轉換爲Int。“123”這個字符串可以轉換爲數字值123,但是字符串“hello world”就不能轉換爲數字值。

let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
// convertedNumber 指向的類型是 "Int?", 或者說 "optional Int" ,可選的int

因爲初始方法可能會失敗,所以它返回的是一個可選的int,而不是int。可選的int(optional Int)寫作:Int?. 後面的問號表示是可選的,即它或許有一個int值,或許根本沒值。(它也不能保存其他如bool或string的值,它要麼有一個int值,要麼沒值)

Implicitly Unwrapped Optionals 隱式解析可選

Sometimes it is clear from a program’s structure that an optional will always have a value, after that value is first set. In these cases, it is useful to remove the need to check and unwrap the optional’s value every time it is accessed, because it can be safely assumed to have a value all of the time.

有時候,從程序中可以確定一個optional 在第一次設置之後 永遠都有一個值,那麼就沒必要每次獲取值的時候都去檢查與解析(check and unwrap),而是可以假設它永遠都有一個值。

These kinds of optionals are defined as implicitly unwrapped optionals. You write an implicitly unwrapped optional by placing an exclamation mark (String!) rather than a question mark (String?) after the type that you want to make optional.

那麼這種類型的optionals就定義爲implicitly unwrapped optionals(隱式解析可選)。隱式解析可選的寫法:在類型的後面加個感嘆號!而不是問號?.

The following example shows the difference in behavior between an optional string and an implicitly unwrapped optional string when accessing their wrapped value as an explicit String:

看兩個例子:

let possibleString: String? = "An optional string."
let forcedString: String = possibleString! // 感嘆號要寫上

let assumedString: String! = "An implicitly unwrapped optional string."
let implicitString: String = assumedString // 不需要感嘆號

第一個例子中,possibleString是一個可選的string,並且被賦了一個值,forcedString 是一個string類型,要把possibleString的值賦給forcedString 就必須對possibleString進行隱式解析,即在後面加個感嘆號。如果不加,會報錯。
第二個例子中,assumedString已經是一個解析過的string類型了,可以直接把值賦給implicitString。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章