Saturday, June 1, 2013

NSMutableArray replaceObjectsAtIndexes example in Objective C (iOS).


NSMutableArray replaceObjectsAtIndexes

Replaces the objects in the receiving array at specified locations specified with the objects from a given array.

- (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects

Parameters
indexes
The indexes of the objects to be replaced.
objects
The objects with which to replace the objects in the receiving array at the indexes specified by indexes. The count of locations in indexes must equal the count of objects.

Discussion of [NSMutableArray replaceObjectsAtIndexes]
The indexes in indexes are used in the same order as the objects in objects.

If objects or indexes is nil this method will raise an exception.

NSMutableArray replaceObjectsAtIndexes example.
NSMutableArray* nulls = [NSMutableArray array];

for (NSInteger i = 0; i < myIndexes.count; i++)
    [nulls addObject:[NSNull null]];

[stageMap replaceObjectsAtIndexes:myIndexes withObjects:nulls];

Example of [NSMutableArray replaceObjectsAtIndexes].
@implementation MyKeys
- (NSUInteger)countOfKeys {
    return [_keys count];
}
- (NSArray *)keysAtIndexes:(NSIndexSet *)indexes {
    return [_keys objectsAtIndexes:indexes];
}
- (void)getKeys:(RTAFile * __unsafe_unretained *)buffer range:(NSRange)inRange {
    [_keys getObjects:buffer range:inRange];
}
- (void)addKeysObject:(RTAFile *)object {
    [_keys addObject:object];
}
- (void)insertKeys:(NSArray *)array atIndexes:(NSIndexSet *)indexes {
    [_keys insertObjects:array atIndexes:indexes];
}
- (void)removeKeysAtIndexes:(NSIndexSet *)indexes {
    [_keys removeObjectsAtIndexes:indexes];
}
- (void)replaceKeysAtIndexes:(NSIndexSet *)indexes withFiles:(NSArray *)array {
    [_keys replaceObjectsAtIndexes:indexes withObjects:array];
}
@end

End of NSMutableArray replaceObjectsAtIndexes example article.