无法在 Swift 中解开 Optional.None 错误

Lol*_*ola 5 ios swift ios8

当为 UILabel 传递值时,会出现错误:

无法解开 Optional.None

源代码:

    @IBOutlet var rowLabel : UILabel
var 行:字符串?{ 已设置{ // 更新视图。 打印(行) rowLabel.text = 行 } }
当我采用新的含义时,UITable 模板中的标签中也会出现错误:
    让 myCell : Cell = Cell(style: UITableViewCellStyle.Subtitle, repeatIdentifier: "cell")
            myCell.myLabel.text = "(indexPath.row)"
    

eXh*_*ted 1

您必须在 willSet 块中使用 newValue,在 didSet 块中使用 oldValue

例子:

class foo {
    var row: String? {
    willSet {
        println("will set value:")
        println(newValue)
    }

    didSet {
        println("did change value:")
        println(oldValue)
    }
    }
}

var bar = foo()

println("First time setter called")
bar.row = "First value"
println("Second time setter called")
bar.row = "Second value"
Run Code Online (Sandbox Code Playgroud)

输出:

First time setter called
will set value:
First value
did change value:
nil
Second time setter called
will set value:
Second value
did change value:
First value
Run Code Online (Sandbox Code Playgroud)