NSMutableArray - Get an array of search index arrays using a string

I worked with NSMutableArray and had no problem returning an object from an array using objectAtIndex:int . Instead of pulling an object from an array with an integer, this is a way to get the index position by searching for an array with a string.

 animalOptions = [[NSMutableArray alloc] init]; //Add items [animalOptions addObject:@"Fish"]; [animalOptions addObject:@"Bear"]; [animalOptions addObject:@"Bird"]; [animalOptions addObject:@"Cow"]; [animalOptions addObject:@"Sheep"]; NSString * buttonTitle = [animalOptions objectAtIndex:1]; // RETURNS BEAR int * objectIndex = [animalOptions object:@"Bear"]; // THIS IS WHAT I NEED HELP WITH, PULLING AN INDEX INTEGER WITH A STRING (ex: Bear) 

I hope this makes sense, and there is an answer, I could not do research on the Internet and find anything through google or apple links.

+9
cocoa nsmutablearray
01 Sep '09 at 17:07
source share
2 answers

You can use the indexOfObject: method from NSArray :

 NSUInteger index = [animalOptions indexOfObject:@"Bear"]; 

If there are duplicate entries, the lowest index of this object is returned. Check out the docs for more information.

+34
Sep 01 '09 at 17:12
source share

you can use [array indexOfObject: object], which will return the index of this object, now with how you want to do this, it may not be wkk, since the string you specify is not the actual string object in the array does this, however , will work

 NSString * buttonTitle = [animalOptions objectAtIndex:1];// RETURNS BEARint objectIndex = [animalOptions indexOfObject:buttonTitle] //this will return 1 
0
01 Sep '09 at 17:15
source share



All Articles