Reputation: 7846
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 searching "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?
Upvotes: 6
Views: 9433
Reputation: 2572
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.
Upvotes: 2
Reputation: 523764
return [theArray indexOfObject:@"ipod"];
Note that NSMutableArray inherits from NSArray, so any NSArray methods can be used on NSMutableArray too.
Upvotes: 9