问题描述
是否有可能在NSArray中找出数组中是否存在给定值(不使用for循环搜索)?任何默认的随机方法。我浏览了文档,但没有找到太多相关性。
Is it possible in an NSArray to find out if a given value exists or not in the array (without searching it using a for loop)? Any default random method. I went through the documentation, but didn't find much relevant.
还请告诉我 valueForKey
方法(我无法从doc获取)。
Please also tell me about valueForKey
method (I was unable to get that from doc).
推荐答案
containsObject:
方法通常会给你你所要求的 - 而它的名称听起来像是在查询特定的实例(即两个具有相同语义值的对象不匹配)它实际上调用 isEqual:
关于对象,所以它通过值进行测试。
The containsObject:
method will usually give you what you're asking - while its name sounds like you are querying for a specific instance (i.e. two object with the same semantic value would not match) it actually invokes isEqual:
on the objects so it is testing by value.
如果你想要项目的索引,正如你的标题所暗示的那样,使用 indexOfObject:
,它还会调用 isEqual:
来查找匹配项。
If you want the index of the item, as your title suggests, use indexOfObject:
, it also invokes isEqual:
to locate the match.
valueForKey:
适用于拥有一系列词典的情况;它在每个字典中查找键并返回结果和数组。
valueForKey:
is for when you have an array of dictionaries; it looks up the key in each dictionary and returns and array of the results.
这篇关于通过传递值来查找NSArray的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!