Skip navigation.
 
mlRe: @distinctUnionOfObjects changes the order of objects?
FROM : Harilaos Skiadas
DATE : Tue Dec 21 20:19:46 2004

--- Scott Anguish <<email_removed>> 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

Related mailsAuthorDate
ml@distinctUnionOfObjects changes the order of objects? Harilaos Skiadas Dec 20, 23:03
mlRe: @distinctUnionOfObjects changes the order of objects? Scott Anguish Dec 21, 10:19
mlRe: @distinctUnionOfObjects changes the order of objects? Harilaos Skiadas Dec 21, 20:19