当为 UILabel 传递值时,会出现错误:
无法解开 Optional.None
源代码:
@IBOutlet var rowLabel : UILabel当我采用新的含义时,UITable 模板中的标签中也会出现错误:
var 行:字符串?{ 已设置{ // 更新视图。 打印(行) rowLabel.text = 行 } }
让 myCell : Cell = Cell(style: UITableViewCellStyle.Subtitle, repeatIdentifier: "cell")
myCell.myLabel.text = "(indexPath.row)"
您必须在 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)
| 归档时间: |
|
| 查看次数: |
2440 次 |
| 最近记录: |