NSMutableArray - 通过使用字符串搜索获取数组索引整数 [英] NSMutableArray - Get Arrays Index Integer By Searching With A String
问题描述
我一直在使用NSMutableArray,并且没有问题通过使用 objectAtIndex:int
从数组中检索对象。
I have been working with NSMutableArray and have had no problems retrieving an object from an array by using objectAtIndex:int
. Rather then pulling an object out of the array by an integer is their a way to get the index position by searching the 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)
希望这有意义,并且有一个答案,我已经无法在线研究,通过谷歌或苹果的类引用找到任何东西。
Hopefully this makes sense, and there is an answer out there, I have been unable to research online and find anything through google or apple's class references.
推荐答案
您可以使用 NSArray
:
NSUInteger index = [animalOptions indexOfObject:@"Bear"];
如果有重复条目,则返回该对象的最低索引。有关详情,请参阅 docs 。
If there are duplicate entries then the lowest index of that object is returned. For more information, take a look at the docs.
这篇关于NSMutableArray - 通过使用字符串搜索获取数组索引整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!