2007年12月6日木曜日

Ordered NSDictionary

Just a no nonscense way of arranging a dictionary based on an ordered set of keys. Remember in Cocoa arranged means ordered.

Use it like this:

NSArray *arrangedValues = [myDictionary arrangedValuesWithArrangedKeys:myOrderedArrayOfKeys];

Here's the category:

@interface NSDictionary (Ordering)
- (NSArray *)arrangedValuesWithArrangedKeys:(NSArray *)orderedKeys;
@end

@implementation NSDictionary (Ordering)

- (NSArray *)arrangedValuesWithArrangedKeys:(NSArray *)orderedKeys
{
NSMutableArray *orderedArrayOfValues = [[[NSMutableArray alloc] init] autorelease];
for (id key in orderedKeys) {
[orderedArrayOfValues addObject:[self objectForKey:key]];
}
return [NSArray arrayWithArray:orderedArrayOfValues];
}

@end

0 件のコメント: