我有一個變量,它接受不同類中數組中對象的數量,我如何跟蹤當前類中這個變量的變化?我做了很多不同的嘗試,但都失敗了。如何觀察價值的變化並展示它?在Swift中
var digitIndex: Int! {
set {
self.digitIndex = newValue
}
get {
return firstClass.indexesOfSelectedNodes().count
}
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if context == &digitIndex {
if let newValue = change?[NSKeyValueChangeNewKey] {
print("newValue")
infoLabel.text = "\(newValue)"
}
}
}
你嘗試這種計算和存儲性能?先回答,然後再回答! – user3441734