init(object:type:index:associatedIndex:)
Creates a change object that represents inserting, removing, or moving an object from an ordered collection at a specific index.
Declaration
init(object anObject: Any?, type: NSCollectionChangeType, index: Int, associatedIndex: Int)Parameters
- anObject:
An optional object the change will remove or insert.
- type:
The type of change.
- index:
The index location within an ordered collection where the change applies.
- associatedIndex:
The index of the change’s counterpart of the opposite type in the diff.
Discussion
Pairs of changes with opposite types that refer to each other represent the index location of their counterpart with the associatedIndex property. Initializing an NSOrderedCollectionDifference with broken associations (or associations that aren’t reflexive) generates an exception. The following example creates a diff where the object @”Red” moves from index 8 to index 3:
NSOrderedCollectionDifference *diff = [[NSOrderedCollectionDifference alloc] initWithChanges:@[
[NSOrderedCollectionChange changeWithObject:@"Red" type:NSCollectionChangeRemove index:8 associatedIndex:3],
[NSOrderedCollectionChange changeWithObject:@"Red" type:NSCollectionChangeInsert index:3 associatedIndex:8]
]];A move pair can have a different object in its removal and insertion changes, which can imply that the change represents moving and changing or replacing an element. Diffs that controller(_:didChangeContentWith:) passes to delegates of NSFetchedResultsController communicate that an object changed even when its position in the results is unaffected.