xDiffCollection
描述
xDiffCollection 是在 bins 的概念上操作的。一个 bin 是一个数组和过滤器。放入那些 bins 的元素必须是可哈希、可标识和可比较的。过滤器根据通过布尔闭包提供的自定义逻辑将元素接受到 bins 中。
xDiffCollection 提供两种操作:更新和删除(如果元素通过测试且之前不存在,则更新可以添加元素)
如果元素已经在那里并且执行了更新操作,会重新运行 bins 中的逻辑来决定这个更新元素是否仍然是合适的 bin。如果不是,该元素将从当前 bin 中删除并移动到一个新的 bin,在该 bin 中更新元素通过另一个 bin 的逻辑。
主要变异方法
mutating func update(element:T) -> DiffCollectionResult
mutating func delete(element:T) -> DiffCollectionResult
元素访问方法
func element(atIndexPath path:IndexPath) -> T?
func numberOfElements(inSection binIndex:Int) -> Int
func numberOfSections() -> Int
示例
在安装 xDiffCollection 后,只需通过以下方式引入:
import xDiffCollection
创建一些 DiffCollectionFilters
let f1 = DiffCollectionFilter<TestObject>(name: "Starts with a", filter:{ s in
if(s.value.starts(with: "a")) {
return true
}
return false
})
let f2 = DiffCollectionFilter<TestObject>(name: "Starts with b", filter:{ s in
if(s.value.starts(with: "b")) {
return true
}
return false
})
let f3 = DiffCollectionFilter<TestObject>(name: "Starts with c", filter:{ s in
if(s.value.starts(with: "c")) {
return true
}
return false
})
使用这些过滤器实例化您的集合
let myCollection = DiffCollection(filters: [f1,f2,f3])
然后开始使用您的集合
let result : DiffCollectionResult = myCollection.update(element: TestObject())
其中
public struct DiffCollectionResult {
public var deleted: [IndexPath]
public var added: [IndexPath]
public var updated: [IndexPath]
}
IndexPath.section 代表 myCollection 中的存储盒,IndexPath.row 代表其盒中元素的位置。
安装
xDiffCollection 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
pod 'xDiffCollection'
作者
ALDO Inc., [email protected]
许可证
xDiffCollection 使用 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。