How to search through a NSMutableArray

主宰稳场 提交于 2019-12-10 13:23:44

问题


I have a NSMutableArray that I need to search for a string and return the key in the array where the string was found. So for example if I'm searching "ipod" and it's the 4th in the array, it would return 3 or whatever position the string is in. What's the best way to do this?


回答1:


return [theArray indexOfObject:@"ipod"];

Reference: http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/indexOfObject:.

Note that NSMutableArray inherits from NSArray, so any NSArray methods can be used on NSMutableArray too.




回答2:


Again from the documentation: Index of Object Passing test

You'll have to write a code block that tests for the substring in each object: NSString rangeOfString: options:

Then you'll get the index of the object with the substring. You'll need to run the string search again for your result, but that should get you what you are after.



来源:https://stackoverflow.com/questions/2609958/how-to-search-through-a-nsmutablearray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!