我正在使用ReactiveKit1.x。它有一个非常有用的功能ObservableCollection<T>,它使您可以监视集合的更改。就我而言,我正在与Dictionary<String,String>
所述的ObservableCollection产生类型的事件CollectionEventChange<T>,并且它包括用于属性insert,update,delete。对于字典,这些是DictionaryIndex<String,String>。
为了检查delete和update条目的内容,似乎我需要保留对先前集合的引用。
是否可以使用这些属性中列出的元素在事件的collection属性中查找更改的条目?
我想念什么吗?(我猜想该解决方案可能与ReactiveKit无关,只是对Swift Dictionaries的一般使用。)
我认为您应该使用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).")
}
}
Run Code Online (Sandbox Code Playgroud)