在 NSArray 中搜索多个项目
我有一个 NSArray 并且许多值具有相同的值(我知道这不是创建数组的最佳方法)。我如何在数组中搜索字符串并让它返回索引数组。例如,如果我想在数组中搜索“DJ Ez”,我怎样才能得到它,以便它返回索引等于该字符串的所有索引?到目前为止,我已经尝试过:
do {
isTheObjectThere = [array containsObject: @"DJ Ez"];
if(isTheObjectThere == true){
indexOfTheObject = [array indexOfObject: @"DJ Ez"];
[arrayOfIndexes addObject:[NSNumber numberWithInt:indexOfTheObject]];
[array removeObjectAtIndex:indexOfTheObject];
NSLog(@"%@", [indexesForAll objectAtIndex:intCtrl]);
hasFinished = false;
}else{
hasFinished = true;
}
intCtrl++;
} while (hasFinished == false);
但是这不起作用,因为当它删除该项目时,它会弄乱下一次搜索的所有索引。我该怎么办?
I have an NSArray and a lot of the values have the same values (i know this isn't the best way to do an array). How can i search for a string within the array and have it return me an array of indexes. For example if i wanted to search the array for "DJ Ez", how can i have it so it returns all the indexes where the index is equal to that string? So far i have tried this:
do {
isTheObjectThere = [array containsObject: @"DJ Ez"];
if(isTheObjectThere == true){
indexOfTheObject = [array indexOfObject: @"DJ Ez"];
[arrayOfIndexes addObject:[NSNumber numberWithInt:indexOfTheObject]];
[array removeObjectAtIndex:indexOfTheObject];
NSLog(@"%@", [indexesForAll objectAtIndex:intCtrl]);
hasFinished = false;
}else{
hasFinished = true;
}
intCtrl++;
} while (hasFinished == false);
However this doesn't work as when it deletes the item it messes up all the indexes for the next search. What do i do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样:
这应该以 NSIndexSet 的形式返回所有匹配的索引,这允许您检查特定索引是否匹配,或者您可以根据需要获取计数并循环匹配的索引。
How about something like this:
That should return all of the matching indexes as an NSIndexSet, which allows you do check if specific indexes matched, or you can get the count and loop through the matching indexes however you wish.