NSMutableIndexSet addIndex
- (void)addIndex:(NSUInteger)index
Parameters of [NSMutableIndexSet addIndex]
index
Index to add.
NSMutableIndexSet addIndex example.
If the indexes are consecutive like in your example, you can use this:
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)];
If not, create a mutable index set and add the indexes (or ranges) one by one:
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
[indexSet addIndex:3];
[indexSet addIndex:5];
[indexSet addIndex:8];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)];
If not, create a mutable index set and add the indexes (or ranges) one by one:
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
[indexSet addIndex:3];
[indexSet addIndex:5];
[indexSet addIndex:8];
Example of [NSMutableIndexSet addIndex].
Keep track as you add values to the index set:
NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
NSUInteger maxIndexs = 0;
NSUInteger indexOfMax = NSNotFound;
for (NSString *subCat in results) {
NSUInteger indexs = 0;
for (NSString *comp in components) {
if ([subCat rangeOfString:comp].location != NSNotFound) {
indexs ++;
}
}
if (indexes > maxIndexs) {
maxIndexs = indexs;
indexOfMax = set.count;
}
[set addIndex:indexs];
}
// find the position of the highest amount of matches in the index set
NSUInteger highestIndex = indexOfMax;
returns = [results objectAtIndex:highestIndex];
NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
NSUInteger maxIndexs = 0;
NSUInteger indexOfMax = NSNotFound;
for (NSString *subCat in results) {
NSUInteger indexs = 0;
for (NSString *comp in components) {
if ([subCat rangeOfString:comp].location != NSNotFound) {
indexs ++;
}
}
if (indexes > maxIndexs) {
maxIndexs = indexs;
indexOfMax = set.count;
}
[set addIndex:indexs];
}
// find the position of the highest amount of matches in the index set
NSUInteger highestIndex = indexOfMax;
returns = [results objectAtIndex:highestIndex];
NSMutableIndexSet addIndex example.
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)]
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)]