bbu*_*s21 9 cocoa nsmutablearray
我一直在使用NSMutableArray,并且通过使用从数组中检索对象没有任何问题objectAtIndex:int.而不是通过整数将对象拉出数组,这是通过用字符串搜索数组来获取索引位置的一种方法.
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)
Run Code Online (Sandbox Code Playgroud)
希望这是有道理的,并且有答案,我无法在线研究并通过谷歌或苹果的课程参考找到任何东西.
Ale*_*ski 34
您可以使用以下indexOfObject:方法NSArray:
NSUInteger index = [animalOptions indexOfObject:@"Bear"];
Run Code Online (Sandbox Code Playgroud)
如果存在重复条目,则返回该对象的最低索引.有关更多信息,请查看文档.