Re: Selecting an object based on the value of one of its attributes
Re: Selecting an object based on the value of one of its attributes
- Subject: Re: Selecting an object based on the value of one of its attributes
- From: Graham Cox <email@hidden>
- Date: Wed, 28 Apr 2010 12:53:09 +1000
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 (email@hidden)
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:
This email sent to email@hidden