[英]How to find out which variable of a tuple was updated at willSet / didSet?
如何找出將在tupleSet / didSet中更新的元組變量?
var myTuple: (a: Int, b: Int) {
didSet {
// Which one was set, a or b?
}
}
實際上,您將使用willSet
獲取更新后的值,並通過將元組的屬性橋接到newValue
( .a
和.b
)來訪問內容:
var myTuple: (a: Int, b: Int) {
willSet {
print(newValue.a)
print(newValue.b)
}
}
您可以比較如下所示的值:
var myTuple: (a: Int, b: Int) {
willSet {
if newValue.a != myTuple.a {
print(".a changed")
}
if newValue.b != myTuple.b {
print(".b changed")
}
}
}
您可以像這樣檢查oldValue
var myTouple: (a: Int, b: Int) {
didSet {
print(oldValue.a)
print(oldValue.b)
}
}
並比較它們。 如果您僅設置雙線束的單個值,例如
myTouple.a = 5
就像您設置了所有內容,其余的都獲得了舊值一樣,就像您寫了一樣
myTouple = (5, myTouple.b)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.