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.
Please also tell me about valueForKey
method (I was unable to get that from doc).
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 invokesisEqual:
on the objects so it is testing by value.If you want the index of the item, as your title suggests, use
indexOfObject:
, it also invokesisEqual:
to locate the match.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.Use
NSPredicate
to filter anNSArray
based onNSDictionary
You can use :
to find index of object.
And to check if object is there or not in array you may use :
Combine
objectAtIndex
andindexOfObject
like this:I believe you want to use the
indexOfObject
method. From the documentation:- (NSUInteger)indexOfObject:(id)anObject