【问题标题】:Swift: Key Value Observing CollectionsSwift:键值观察集合
【发布时间】:2014-11-12 18:49:09
【问题描述】:

我读过Swift documentation on Key-Value Observing

但是,如何在 Swift 中实现集合的键值观察?

以下是 Swift 缺少的相关 Object-C 文档:

【问题讨论】:

    标签: ios swift collections key-value-observing key-value-coding


    【解决方案1】:

    这对我有用:

    class Document: NSDocument {
    
        var rooms = [Room]()
    
        func insertObject(room: Room, inRoomsAtIndex index: Int)
        {
            // Do something interesting 
            NSLog("Adding \(room) to \(rooms)")
    
            // Perform the actual insert
            rooms.insert(room, atIndex: index)
        }
    
        func removeObjectFromRoomsAtIndex(index: Int)
        {
            let room = rooms[index]
            NSLog("Removing room \(room)")
    
    
            // Perform the actual remove
           rooms.removeAtIndex(index)
        }
    
        // ...
    }    
    

    我通过将预期的 Obj-C KV 签名转换为我感兴趣的方法的 Swift 来实现这一点。您应该能够让其他方法(对于无序对多)以相同的方式工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多