Optional
這東西是Swift特有的東西,在程式碼裡面會看到許多驚嘆號(!)與問號(?),就是來處理這東西的。
當一個值可能為nil的時候,就必須使用optional來標記它,也就是使用問號
舉一個生活上的例子來說,現在要去領錢,可是不確定還有沒有錢可以領?
這個錢可能有值(有錢可領),可能沒值(沒錢可領),你就必須在這個錢後面加入一個問號
var money : Int?
當你確定這個optional有值的時候,使用驚嘆號(!)來解開它,如果你確定這個值並不是nil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//不確定有沒有值 | |
var money : Int? | |
//有三百塊錢 | |
money = 300 | |
//確定有值,使用! 有三百塊太爽啦! | |
print(money!) |
在舉一個程式上的例子,許多時候可能會想將字串轉成數值,可是並不是所有的字串都可以轉成數值,在Swift當中,字串轉數值會回傳一個optional,也許有值,也許沒值。
這樣一來應該對optional有初步的認識了。
接著你可以與If一起使用,分兩種,判斷後在解開,跟若是能解開,直接指派值。
此外,你也可以使用兩個問號(??),搭配一個變數來使用,請參考以下的程式碼
接著,nil的指派也有分幾種,大前提是這個變數必須是optional,否則不能給nil
隱性解析optional(Implicitly Unwrapped Optionals)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//字串 | |
var myString = "123" | |
//透過Int()將字串轉成數字 | |
var number = Int(myString) | |
//確定這個值是數字,用!標記解開optional | |
print(number!) | |
//字串二 | |
var myString2 = "Apple" | |
//透過Int()將字串轉成數字 | |
var number2 = Int(myString2) | |
//無法使用!標記解開,因為該值為nil | |
print(number2) |
這樣一來應該對optional有初步的認識了。
接著你可以與If一起使用,分兩種,判斷後在解開,跟若是能解開,直接指派值。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myString = "123" | |
var number = Int(myString) | |
//如果optional有值的話,就會認定不等於 nil | |
if number != nil { | |
print(number!) | |
} | |
//如果Int()回傳的optional包含值的話,將該值指派給actualNumber,且不必再使用!解開optional | |
if let actualNumber = Int(myString){ | |
print(actualNumber) | |
} |
此外,你也可以使用兩個問號(??),搭配一個變數來使用,請參考以下的程式碼
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var optionalName: String? = "Tom" | |
//使用兩個問號來解包,如果此optional變數有值,則解包後給予,若沒值,則使用??右邊的值 | |
var name = optionalName ?? "請輸入名稱" | |
print(name) //Tom |
接著,nil的指派也有分幾種,大前提是這個變數必須是optional,否則不能給nil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var number : Int? = 123 | |
//number = nil | |
number = nil | |
//number2 = nil | |
var number2: Int? | |
//number3 非 optional 不能指派為nil | |
var number3: Int = nil |
在宣告的時候就給驚嘆號(!),這樣就不必在解析的時候加入驚嘆號
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//自動解析optional | |
let assumedString: String! = "An implicitly unwrapped optional string." | |
//不需再使用! | |
print(assumedString) |
當然你一樣可以針對這個optional做If的判斷之類的。
使用隱性解析optional時,你必須確定你在中途都不會將它改為nil,否則使用一般的
optional就好了。
那麼你可能會想,這東西要用在什麼時機點呢,主要是當你這個元件在創建的時期允許它為
nil,然後在非常早的階段就使它為非nil,當optional非nil要解析的時候,要使用驚嘆號標
記,每次使用都要標也太累人,因此使用隱性解析,在一開始就解開了,之後就不用使用驚
嘆號標記了。
沒有留言:
張貼留言