If you use NEEDS to use an array, skip a little lower
Alternatives
Other options may include:
Using NSDictionary , which uses key-> value pairs that (I expect) will have O (1) reading complexity due to the extra key storage space
If you do not use duplicates and the order is not important, using NSSet will offer a higher degree of reading (I do not know what complexity will be, documents will probably be)
Using array
If you save the array, you can search in O(log n) time instead of O(n) , since you can use binary search.
Caveat Lector : it was written into memory
-(void) { int proposedIndex = 0; proposedIndex = [array indexOfObject:node inSortedRange:NSMakeRange(0, array.count) options:NSBinarySearchingInsertionIndex usingComparator: ^ NSComparisonResult(id obj1, id obj2) { if (obj1.valueToCompare < obj2.valueToCompare) return NSOrderedAscending; if (obj1.valueToCompare > obj2.valueToCompare) return NSOrderedDescending; else return NSOrderedSame; }]; [array insertObject:node atIndex:proposedIndex]; } -(id) { int location = [array indexOfObject:node inSortedRange:NSMakeRange(0, array.count) options:NSBinarySearchingFirstEqual usingComparator: ^ NSComparisonResult(id obj1, id obj2) { if (obj1.valueToCompare < obj2.valueToCompare) return NSOrderedAscending; if (obj1.valueToCompare > obj2.valueToCompare) return NSOrderedDescending; else return NSOrderedSame; }]; if (location == NSNotFound) return nil; return [array objectAtIndex:location]; }
James webster
source share