On Apr 27, 2010, at 8:06 PM, Roland King wrote:

> Graham Cox wrote:
>> 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
> 
> I think that as Graham suggests that would be slower than searching yourself, 
> but it is a method and it does exist and it's "free", so you could try that 
> and if it's fast enough for you, that's great.
> 
> Actually iterating the list however yourself is very simple  and possibly 
> only the same number of lines of code as making a predicate. (code typed in 
> mail)
> 
> YourObject *found = nil;
> for( YourObject *obj in yourArrayOfObjects )
>    if( [ [ obj thePropertyYouWant ] isEqualToString:yourThing ] )
>    {
>        found = obj;
>        break;
>    }
> // if found isn't nil, you found one, if it is, you failed.
> 
> Finally - are these all your objects and are you always looking for the same 
> property of them? If so instead of dumping them into an array you could build 
> a dictionary of them as you insert them, keyed by that string property, then 
> go look it up later when you want it.
GREAT IDEA ! I'll try it. Thanks to you and Graham.
Lynn
_______________________________________________

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