Reputation: 33684
I have a NSMutableIndexSet and I want to be able to add and store the index set by 1, planning to use a block to do it, here's what I have so far:
[indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
//add and store back the set here
idx++;
}];
is there a way to iterate and modify at the same time? if yes how? the code above doesn't seem to work
Upvotes: 1
Views: 888
Reputation: 112873
You can not enumerate and modify the NSMutableIndexSet you are enumerating. Just create a new NSMutableIndexSet and add the entries to it. It is doubtful that there is a performance hit.
Example (with ARC):
NSMutableIndexSet *originalIndexSet = [NSMutableIndexSet new];
[originalIndexSet addIndex:1];
[originalIndexSet addIndex:5];
NSMutableIndexSet *newIndexSet = [NSMutableIndexSet new];
[originalIndexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
[newIndexSet addIndex:idx+1];
}];
NSLog(@"originalIndexSet: %@", originalIndexSet);
NSLog(@"newIndexSet: %@", newIndexSet);
Then
originalIndexSet = newIndexSet;
NSLog output:
originalIndexSet: [number of indexes: 2 (in 2 ranges), indexes: (1 5)]
newIndexSet: [number of indexes: 2 (in 2 ranges), indexes: (2 6)]
Upvotes: 4