在该代码时,该文本的变化,titleEditingChanged
被调用(如预期)。但是,当它执行线为什么'didset'在设置属性属性时会调用属性?
investment?.title = sender.text!
它调用的Investment
的didset{}
。 为什么?
class InvestmentCell: UITableViewCell {
var investment: Investment? {
didSet {
// setup UI elements from class properties
textField.text = investment?.title
valueField.text = investment?.value?.description
}
}
@IBAction func titleEditingChanged(sender: UITextField) {
investment?.title = sender.text!
}
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var valueField: UITextField!
}
“投资”是类还是结构? – EmilioPelaez
这是正常行为。当你将一个项目追加到一个数组时,也会发生同样的情况。 –
'didSet'和'willSet'是属性观察者*。你可以在[Swift Book](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html)中准备好他们。 –