Diff
public struct Diff<T, C>: Collection where
T: Equatable & Hashable,
C: RangeReplaceableCollection,
C.Element: Collection,
C.Element.Element == T,
C.Index == Int,
C.Element.Index == C.Index
extension Diff: MutableCollection
Diff is a concept of a container for multiple collections and provides methods to access elements of those collections
Example:
let names = ["Adams", "Bryant", "Channing"]
let cars = ["Ford", "Dodge"]
let collection = Diff([names,cars])
let subSequence = collection[1]
print(subSequence)
// ["Ford", "Dodge"]
let element = collection[IndexPath(row: 0,section: 1)]
print(element)
// "Ford"
Contains a very powerful extension for Diffwhere C == [CollectionBin<[T]>] that
provides logic for updating element and putting it in propper subcollections of type CollectionBin<[T]>
An update operation returns DiffCollectionSnapshot<T> containing modifications made to the collection and
its subcollections.
Usage Example
Define a model
//let's have a model like struct TestObject: Hashable, Equatable { var value: String var status: ObjectStatus var rank: Int func hash(into hasher: inout Hasher) { hasher.combine(value) hasher.combine(rank) } // -Note: since value and rank are a part of unique value of the object the only part that // cab be changed is the status static func == (lhs: Self, rhs: Self) -> Bool { lhs.status == rhs.status } }Define filters for sections:
let startsARankSorted = DiffCollectionFilter<TestObject>(name: "Starts with a",
filter: { $0.value.starts(with: "a") },
sort: { $0.rank > $1.rank })
let startedBValueSorted = DiffCollectionFilter<TestObject>(name: "Starts with b",
filter: { $0.value.starts(with: "b") },
sort: { $0.value > $1.value })
- initialize
Diff
var diffCollection = [startsARankSorted, startedBValueSorted]
- Start using by calling upade function
let elementA = TestObject(value: "Arm",
status: .new,
rank: 100)
let snapshot = diffCollection.update(with: elementA)
debugPrint(snapshot.changes)
// updatedIndexes = [], removedIndexes = [], addedIndexes = [IndexPath(row: 0, section: 0)]
-
Start index of the
CDeclaration
Swift
public var startIndex: C.Index { get } -
End index of the
CDeclaration
Swift
public var endIndex: C.Index { get } -
Declaration
Swift
public typealias Element = C.Element -
Undocumented
Declaration
Swift
public typealias IndexType = C.Index -
Undocumented
Declaration
Swift
public init(collection: C) -
Geter and setter of the element specified by an
IndexType.let names = ["Adams", "Bryant", "Channing"] let cars = ["Ford", "Dodge"] let collection = Diff([names,cars]) let element = collection[1] print(element) // ["Ford", "Dodge"]Complexity
O(n)
Parameters
IndexA index of the element(
C). The index must be valid. -
Returns the position immediately after the given index.
The successor of an index must be well defined. For an index
iinto a collectionc, callingc.index(after: i)returns the same index every time.Parameters
iA valid index of the collection.
imust be less thanendIndex.Return Value
The index value immediately after
i. -
Accesses the element specified by an
IndexPath.let names = ["Adams", "Bryant", "Channing"] let cars = ["Ford", "Dodge"] let collection = Diff([names,cars]) let element = collection[IndexPath(row: 0,section: 1)] print(element) // "Ford"Complexity
O(n)
Declaration
Swift
public subscript(position: IndexPath) -> T? { get }Parameters
IndexA index of the element(
C). The index must be valid.
-
Constructor that uses a collection of
DiffCollectionFilter<T>to specify rules per every sectionDeclaration
Swift
init(filters: [DiffCollectionFilter<T>])Parameters
filters -
Undocumented
Declaration
Swift
func updating(with element: T) -> (collection: Diff, changes: DiffCollectionResult) -
The main core function of the `Diff`. Based on the settings `DiffCollectionFilter<T>` for subcollections `CollectionBin<[T]>` will add, remove or update element in those subcollectionsDeclaration
Swift
@discardableResult mutating func update(with element: T) -> DiffCollectionSnapshot<T>Parameters
elementElement of Type
TReturn Value
DiffCollectionSnapshot
-
The main core function of the
Diff. Based on the settingsDiffCollectionFilter<T>for subcollectionsCollectionBin<[T]>will add, remove or update element in those subcollectionsDeclaration
Swift
func updating(using element: T) -> DiffCollectionSnapshot<T>Parameters
elementElement of Type
TReturn Value
DiffCollectionSnapshot
Diff Structure Reference