我使用的是ReactiveKit 1.x。它有一个非常有用的ObservableCollection<T>,它允许您监视对集合的更改。在我的例子中,我将它与Dictionary<String,String>一起使用
ObservableCollection生成一个类型为CollectionEventChange<T>的事件,它包括insert、update、delete的属性。对于字典,这些是DictionaryIndex<String,String>。
为了检查delete和update条目的内容,我似乎需要保持对前面集合的引用。
是否可以使用这些属性中列出的元素来查找事件的collection 属性中更改的条目?
我是不是遗漏了什么?(我想这个解决方案可能与ReactiveKit没有任何关系,只是对Swift字典的普遍使用。)
发布于 2016-05-14 16:25:25
我认为您应该使用zipPrevious方法来获取以前的事件,并从它获得以前的集合。
let collection = ObservableCollection(["key": "value"])
collection.zipPrevious().observe { (old, new) in
guard let old = old else { return } // `old == nil` on first event
for index in new.deletes {
let value = old.collection[index]
print("Deleted \(value).")
}
}https://stackoverflow.com/questions/37228625
复制相似问题