Re: @distinctUnionOfObjects changes the order of objects?
Re: @distinctUnionOfObjects changes the order of objects?
- Subject: Re: @distinctUnionOfObjects changes the order of objects?
- From: Harilaos Skiadas <email@hidden>
- Date: Tue, 21 Dec 2004 11:19:46 -0800 (PST)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
--- Scott Anguish <email@hidden> wrote:
> you misunderstand what @distinctUnionOfObjects gives
> you
>
> it'll give you an array of uniqued values in
> ProgramCode, and you
> can't guarantee the order to be preserved
I see. So in case there are dublicates in ProgramCode,
it will only keep one of them? Hence there isn't even
a notion of order in the first place.
> you could keep a separate NSDictionary that
> contains the ProgramCode
> string as the key, and the object in the array as
> the value. then you
> could do the lookup in two stages (look up
> ProgramCode in the
> dictionary, and then get ProgramName from the value
> returned
>
This happens way too often in my program, and that's
probably a design flaw but for now I am trying to get
it to work, and I'd rather not do the two stage thing
all the time. Plus, I often just want the list of the
ProgramCode's in the same order each time.
I ended up adding a category on NSArray with methods
valuesForKey:(NSString *)aKey, which returns an array
with the values for aKey of the objects of the array,
and objectWithValue:(id)aValue forKey:(NSString
*)aKey, which finds and returns the first object in
the array with value aValue for key aKey. Seems to
work fine for now, hopefully I haven't made any very
serious mistakes. Here is their code:
- (id)objectWithValue:(id)aValue forKey:(NSString
*)aKey
{
int i;
unsigned int count = [self count];
id anItem;
for (i = 0; i < count; i++) {
anItem = [self objectAtIndex:i];
if ([[anItem valueForKey:aKey] isEqual:aValue])
return anItem;
}
return nil;
}
- (NSArray *)valuesForKey:(NSString *)aKey
{
NSMutableArray *tempArray = [NSMutableArray array];
int i;
unsigned int count = [self count];
for (i = 0; i < count; i++) {
[tempArray addObject:[[self objectAtIndex:i]
valueForKey:aKey]];
}
return tempArray;
}
Thanks,
Haris
__________________________________
Do you Yahoo!?
All your favorites on one personal page Try My Yahoo!
http://my.yahoo.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden