NSArray

From GNUstepWiki
Revision as of 15:54, 21 April 2006 by Cbv (talk | contribs) (NSArray of course is in Foundation, not AppKit...)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Well ... array.

Code chunks

Create index dictionary

Following NSArray category method creates a dictionary which works as an index by an attribute of contained objects. It uses Key Value Coding.

- (NSDictionary *)indexDictionaryForKey:(NSString *)key
{
   NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
   NSDictionary        *retval;
   NSEnumerator        *enumerator;
   id                   object;

   enumerator = [self objectEnumerator];

   while( (object = [enumerator nextObject]) )
   {
       [dict setObject:object forKey:[object valueForKey:key]];
   }

   retval = [[NSDictionary alloc] initWithDictionary:dict];
   RELEASE(dict);
   
   return AUTORELEASE(retval);
}