On 28/04/2010, at 12:37 PM, Lynn Barton wrote:

> Newbie question: Consider an array of dictionary objects, all of the same 
> class. One of the ivars of that class is an NSString which is unique for each 
> instance. Does there already exist a method that will identify the one 
> dictionary object that has a given value of that ivar, without me having to 
> write code to examine all of the objects one by one? I have searched the 
> documentation without finding such a method.
> Lynn Barton


You could use NSPredicate to "filter" your collection based on your unique 
string property being equal to the one sought. If they are unique it will 
return exactly one item (or none, if it doesn't exist).

See [NSArray filteredArrayUsingPredicate:]

It's unclear whether that would be actually any faster than doing a linear 
search yourself - it might be slower, in that it wouldn't return as soon as it 
found the item, but would always check every element.

--Graham


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to