gpt4 book ai didi

cocoa - NSMutableArray - 通过搜索字符串获取数组索引整数

转载 作者:行者123 更新时间:2023-12-03 16:04:59 25 4
gpt4 key购买 nike

我一直在使用 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)

希望这是有道理的,并且有一个答案,我无法在线研究并通过谷歌或苹果的类引用找到任何东西。

最佳答案

您可以使用 NSArray 中的 indexOfObject: 方法:

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

如果存在重复条目,则返回该对象的最低索引。欲了解更多信息,请查看docs .

关于cocoa - NSMutableArray - 通过搜索字符串获取数组索引整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1363593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com