NSArray

From GNUstepWiki

(Difference between revisions)
Revision as of 21:24, 16 February 2005
Stefan Urbanek (Talk | contribs)

← Previous diff
Revision as of 09:17, 17 February 2005
Stefan Urbanek (Talk | contribs)
added a reference to the KVC
Next diff →
Line 5: Line 5:
=== Create index dictionary === === Create index dictionary ===
-Following method creates a dictionary which works as index by an attribute of contained objects.+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 - (NSDictionary *)indexDictionaryForKey:(NSString *)key

Revision as of 09:17, 17 February 2005

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);
}