2017-04-07 50 views
-3

在下面的代碼中,爲什麼5沒有賦值給「somevar」?爲什麼隱式解包可選未分配?

class ViewController: UIViewController { 

    var somevar : Int? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     somevar! = Int(5) // why 5 is not assigned to somevar here 
    } 
} 

背景:

somevar被聲明爲可選變量,這意味着如果該變量是零則命令使用此變量將被忽略。

例子:

class ViewController: UIViewController { 

    var somevar : Int? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     somevar? = 5 // this command will be ignored bcz somevar is nil 
    } 
} 

要在我們自己的風險強行執行的命令,我們使用「隱式展開可選」,使我們確信,命令將被執行被執行,在這種情況下,下面一行

somevar! = 5 

fatal error: unexpectedly found nil while unwrapping an Optional value

在執行這行,爲什麼「5」不分配給「somevar」,而不是出現致命錯誤?

class ViewController: UIViewController { 

    var somevar : Int? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     somevar! = 5 
    } 
} 
+0

爲var賦值的正確方法是'somevar = 5'而不是'somevar? = 5' –

回答

2

當我們做something!(強調馬克),我們是 「力閱讀」(力展開)可選。

也就是說,在爲其分配新值之前,上面的代碼嘗試讀取something
由於somethingnil,代碼爆炸。

爲了說明:

var somevar: Int? 

print(somevar!) 
// Code explodes! 

print(somevar) 
// Output is "nil" 

somevar = 5 

print(somevar!) 
// Output is "5" 

print(somevar) 
// Output is "Optional(5)" 

作爲@LeoDabus說明,本是覆蓋在Apple's awesome Swift book
(BTW一個很好的書!❤️)

+0

謝謝你,反斜槓! – user2259784

+1

@LeoDabus考慮到這個問題並沒有什麼區別,但我明白你的觀點並相應地改變了我的答案。謝謝。 –

+0

另外「力量閱讀」(力量解開)=金子! :)(提及澄清了很多) 翻閱本書 – user2259784

0

給予一定的顏色是什麼somevar? = 5在做什麼。

//: Playground - noun: a place where people can play 

import Foundation 

// In swift you have to unwrap an optional before you can do anything with it 
var x: Int? = 1 
var y: Int? = 2 

// So you can't do this 
//var z = x + y 

// You have to do this 
if let x = x, 
    let y = y { 
    // Here x and y are no longer of the type Int? they are of the type Int 
    var z = x + y 
} 

// You don't have to name them the same 
if let someX = x, 
    let someY = y { 
    // Here x and y are no longer of the type Int? they are of the type Int 
    var z = someX + someY 
} 

// This can be a pain sometimes if you want to "do nothing" in the nil case, or want to unwrap something multiple "levels" 
// of optionals deep. For example: 

struct Pet { 
    let name: String 
} 

struct Person { 
    let pet: Pet? 
} 

var person: Person? = Person(pet: Pet(name: "Rex")) 

// To get the person's pet's name we have to unwrap a few things 
if let person = person, 
    let pet = person.pet { 
    print("The pet's name is \(pet.name)") 
} 

// We can do this a little easier by using "Optional Chaining" 
if let name = person?.pet?.name { 
    print("The pet's name is \(name)") 
} 

// So here's where your problem comes in 
var number: Int? = nil 

// This is "optional chaining" the assignment of 5 to number. But, because number is current nil the assignment won't happen. 
number? = 5 

// However 
number = 5 

// Now the number is 5 

number? = 10 

// Now the number is 10, because the optional chaining succeded because number was not nil. All this being said, I've never 
// seen someone use x? = 5 in production code, and I can't think of a reason to do that. Just do x = 5 like the other 
// answers have said. 

TL; DR,somevar? = 5使用可選的鏈接,只設置somevar5如果不是nil

+1

謝謝安德魯的解釋。 – user2259784

相關問題