一尘不染

如何使用属性观察器观察Swift集合类型的特定元素?

swift

回答“ 我如何知道数组内元素的值是否已更改”这一问题时受到启发 ,答案是使用
属性观察 器检查数组是否已被修改。

但是,如何确定属性查看器中集合类型中的已更新元素是什么?例如:

class MyClass {
    var strings: [String] = ["hello", "world", "!"] {
        didSet(modifiedStrings) {
            print("strings array has been modified!!:")
            print(modifiedStrings)
        }
    }
}

let myClass = MyClass()
myClass.strings.append("a string")
myClass.strings[0] = "Hello"
myClass.strings.removeLast()

请注意,didSet每次添加,更新或删除操作都已调用该代码,但是我如何才能确切知道受影响的元素是什么?有没有办法通过将strings数组除以
Property Observer 来实现这一目标?

我问的是Swift中的所有集合类型,因为我认为对于所有集合类型都应该是相同的行为,这与观察有关。

谢谢。


阅读 196

收藏
2020-07-07

共1个答案

一尘不染

感谢@hnh,根据他的回答,我最终得到了:

class MyNumber: NSObject {

    // NOTE that it works in both "willSet" and "didSet"

    /// Array ///
    var arrayNumbers: [String] = ["one", "two", "three"] {
        willSet {
            let oldStrings = Set(arrayNumbers)
            let newStrings = Set(newValue)

            print("removed from array: \(oldStrings.subtracting(newStrings))")
            print("added to array:   \(newStrings.subtracting(oldStrings))")

            print("----------")
        }
    }

    /// Set ///
    var setNumbers: Set = ["one", "two", "three"] {
        didSet(newSet) {
            print("removed from set: \(newSet.subtracting(setNumbers))")
            print("added to set:   \(setNumbers.subtracting(newSet))")

            print("----------")
        }
    }

    var dictionaryNumbers = ["1": "one", "2": "two", "3": "three"] {
        didSet(modified) {
            let oldKeys = Set(dictionaryNumbers.keys)
            let newKeys = Set(modified.keys)

            let oldValues = Set(dictionaryNumbers.values)
            let newValues = Set(modified.values)

            print("removed from dictionary (keys): \(newKeys.subtracting(oldKeys)) (values): \(newValues.subtracting(oldValues))")
            print("added to dictionary (keys):   \(oldKeys.subtracting(newKeys)) (values):    \(oldValues.subtracting(newValues))")
            print("----------")

//            print("removed (values): \(newValues.subtracting(oldValues))")
//            print("added (values):   \(oldValues.subtracting(newValues))")

        }
    }
}

执行:

let myNumber = MyNumber()

/// Array ///

// adding:
myNumber.arrayNumbers.append("four")
/* Logging:
 removed: [] means that nothing has been removed form the array
 added:   ["four"]
 ----------
 */

// updating:
myNumber.arrayNumbers[0] = "One"
/* Logging:
 removed: ["one"]
 added:   ["One"]
 ----------
 */

// deleting:
myNumber.arrayNumbers.removeLast()
/* Logging:
 removed: ["four"]
 added:   [] means that nothing has been added to the array
 ----------
 */


/// Set ///

// adding:
myNumber.setNumbers.insert("four")
/* Logging:
 removed from set: [] means that nothing has been removed form the set
 added to set:   ["four"]
 ----------
 */

// deleting:
myNumber.setNumbers.removeFirst()
/* Logging:
 removed from set: ["three"] // sets are unsorted...
 added to set:   [] means that nothing has been added to the set
 ----------
 */


/// Dictionary ///

// adding:
myNumber.dictionaryNumbers["4"] = "four"
/* Logging:
 removed from dictionary (keys): [] (values): []
 added to dictionary (keys):   ["4"] (values):    ["four"]
 ----------
 */

// updating:
myNumber.dictionaryNumbers["1"] = "One"
/* Logging:
 removed from dictionary (keys): [] (values): ["one"]
 added to dictionary (keys):   [] (values):    ["One"]
 ----------
 */

// deleting:
myNumber.dictionaryNumbers.removeValue(forKey: "2")
/* Logging:
 removed from dictionary (keys): ["2"] (values): ["two"]
 added to dictionary (keys):   [] (values):    []
 ----------
 */

这显示了如何处理数组,集合和字典。

2020-07-07