Raphael Caixeta
Raphael Caixeta

Reputation: 7846

How to search through a NSMutableArray

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

Answers (2)

Luke
Luke

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

kennytm
kennytm

Reputation: 523764

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.

Upvotes: 9

Related Questions