[英]How to tell if object is in NSArray?
Is there a way to tell if a certain object is in an NSArray?有没有办法判断某个 object 是否在 NSArray 中? The way I am adding objects to my array makes it possible for the same object to be added multiple times and I wanted to see if there was a way to see if it was already there (anywhere) in that array.
我将对象添加到我的数组的方式使得可以多次添加相同的 object,我想看看是否有办法查看它是否已经存在于该数组中(任何地方)。
The NSArray containsObject:
method is precisely for this purpose, its full signature being: NSArray
containsObject:
方法正是为此目的,它的完整签名是:
- (BOOL)containsObject:(id)anObject
See the full NSArray Class Reference docs for more information.有关更多信息,请参阅完整的NSArray Class 参考文档。
if([yourArray indexOfObject:yourObject] == NSNotFound) {
// your object is not in here
}
Edit : middaparkas approach is way better (if you don't want the index …)!编辑:middaparkas 方法更好(如果你不想要索引......)!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.