I have a NSMutableArray that I need to search for a string and return the key in the array where the string was found. So for example if I'm searchi开发者_运维问答ng "ipod" and it's the 4th in the array, it would return 3 or whatever position the string is in. What's the best way to do this?
return [theArray indexOfObject:@"ipod"];
Reference: http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/indexOfObject:.
Note that NSMutableArray inherits from NSArray, so any NSArray methods can be used on NSMutableArray too.
Again from the documentation: Index of Object Passing test
You'll have to write a code block that tests for the substring in each object: NSString rangeOfString: options:
Then you'll get the index of the object with the substring. You'll need to run the string search again for your result, but that should get you what you are after.
精彩评论