• <small id='wwjEl'></small><noframes id='wwjEl'>

      <i id='wwjEl'><tr id='wwjEl'><dt id='wwjEl'><q id='wwjEl'><span id='wwjEl'><b id='wwjEl'><form id='wwjEl'><ins id='wwjEl'></ins><ul id='wwjEl'></ul><sub id='wwjEl'></sub></form><legend id='wwjEl'></legend><bdo id='wwjEl'><pre id='wwjEl'><center id='wwjEl'></center></pre></bdo></b><th id='wwjEl'></th></span></q></dt></tr></i><div id='wwjEl'><tfoot id='wwjEl'></tfoot><dl id='wwjEl'><fieldset id='wwjEl'></fieldset></dl></div>
      1. <legend id='wwjEl'><style id='wwjEl'><dir id='wwjEl'><q id='wwjEl'></q></dir></style></legend>

          <bdo id='wwjEl'></bdo><ul id='wwjEl'></ul>
      2. <tfoot id='wwjEl'></tfoot>

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

        时间:2023-07-08

        <small id='vyiQi'></small><noframes id='vyiQi'>

        <i id='vyiQi'><tr id='vyiQi'><dt id='vyiQi'><q id='vyiQi'><span id='vyiQi'><b id='vyiQi'><form id='vyiQi'><ins id='vyiQi'></ins><ul id='vyiQi'></ul><sub id='vyiQi'></sub></form><legend id='vyiQi'></legend><bdo id='vyiQi'><pre id='vyiQi'><center id='vyiQi'></center></pre></bdo></b><th id='vyiQi'></th></span></q></dt></tr></i><div id='vyiQi'><tfoot id='vyiQi'></tfoot><dl id='vyiQi'><fieldset id='vyiQi'></fieldset></dl></div>
        • <bdo id='vyiQi'></bdo><ul id='vyiQi'></ul>

            <legend id='vyiQi'><style id='vyiQi'><dir id='vyiQi'><q id='vyiQi'></q></dir></style></legend>

                  <tbody id='vyiQi'></tbody>

                1. <tfoot id='vyiQi'></tfoot>
                  本文介绍了如何使用属性观察器观察具有 Swift 集合类型的特定元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在回答 我如何知道数组中某个元素的值是否被更改?,答案是使用 Property Observer 来检查数组是否已被修改.

                  Inspired when answering the question of How do I know if a value of an element inside an array was changed?, The answer was using a Property Observer for checking if an array has been modified.

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

                  However, How can I determine what is/are the updated element(s) in a collection type in a property observer? For example:

                  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 来实现这一点?

                  Note that the didSet code has been called for each of adding, updating or deletion operation, but how can I exactly know what are the effected elements? is there even a way to achive this by decalring strings array as a Property Observer?

                  我在询问 Swift 中的所有集合类型,因为我认为它们应该是相同的行为,它是关于观察的.

                  I am asking about all collection types in Swift because I assume that it should be the same behavior for all of them, it is about the observing.

                  谢谢.

                  推荐答案

                  感谢@hnh,基于他的回答,我最终得到:

                  Thanks for @hnh, based on his answer, I ended up with:

                  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):    []
                   ----------
                   */
                  

                  这显示了如何处理数组、集合和字典.

                  This shows how to deal with array, set and dictionary.

                  这篇关于如何使用属性观察器观察具有 Swift 集合类型的特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:android - 从网络服务器保存图像并将其设置为壁纸 下一篇:减少数组以在 Swift 中设置

                  相关文章

                    <bdo id='X8Vxd'></bdo><ul id='X8Vxd'></ul>
                2. <tfoot id='X8Vxd'></tfoot>

                  <small id='X8Vxd'></small><noframes id='X8Vxd'>

                  <legend id='X8Vxd'><style id='X8Vxd'><dir id='X8Vxd'><q id='X8Vxd'></q></dir></style></legend>

                      <i id='X8Vxd'><tr id='X8Vxd'><dt id='X8Vxd'><q id='X8Vxd'><span id='X8Vxd'><b id='X8Vxd'><form id='X8Vxd'><ins id='X8Vxd'></ins><ul id='X8Vxd'></ul><sub id='X8Vxd'></sub></form><legend id='X8Vxd'></legend><bdo id='X8Vxd'><pre id='X8Vxd'><center id='X8Vxd'></center></pre></bdo></b><th id='X8Vxd'></th></span></q></dt></tr></i><div id='X8Vxd'><tfoot id='X8Vxd'></tfoot><dl id='X8Vxd'><fieldset id='X8Vxd'></fieldset></dl></div>