将NSArray转换为NSDictionary
我如何将NSArray
转换为NSDictionary
,使用数组对象的int字段作为NSDictionary
键?
- (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array { id objectInstance; NSUInteger indexKey = 0U; NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init]; for (objectInstance in array) [mutableDictionary setObject:objectInstance forKey:[NSNumber numberWithUnsignedInt:indexKey++]]; return (NSDictionary *)[mutableDictionary autorelease]; }
试试这个魔法:
NSDictionary *dict = [NSDictionary dictionaryWithObjects:records forKeys:[records valueForKey:@"intField"]];
仅供参考,这是可能的,因为这个内置function:
@interface NSArray(NSKeyValueCoding) /* Return an array containing the results of invoking -valueForKey: on each of the receiver's elements. The returned array will contain NSNull elements for each instance of -valueForKey: returning nil. */ - (id)valueForKey:(NSString *)key;
这为NSArray
添加了一个类别扩展。 需要C99
模式(这是目前的默认,但以防万一)。
在.h
文件的某处可以#import
所有..
@interface NSArray (indexKeyedDictionaryExtension) - (NSDictionary *)indexKeyedDictionary @end
在.m
文件中
@implementation NSArray (indexKeyedDictionaryExtension) - (NSDictionary *)indexKeyedDictionary { NSUInteger arrayCount = [self count]; id arrayObjects[arrayCount], objectKeys[arrayCount]; [self getObjects:arrayObjects range:NSMakeRange(0UL, arrayCount)]; for(NSUInteger index = 0UL; index < arrayCount; index++) { objectKeys[index] = [NSNumber numberWithUnsignedInteger:index]; } return([NSDictionary dictionaryWithObjects:arrayObjects forKeys:objectKeys count:arrayCount]); } @end
使用示例:
NSArray *array = [NSArray arrayWithObjects:@"zero", @"one", @"two", NULL]; NSDictionary *dictionary = [array indexKeyedDictionary]; NSLog(@"dictionary: %@", dictionary);
输出:
2009-09-12 08:41:53.128 test[66757:903] dictionary: { 0 = zero; 1 = one; 2 = two; }
这是从员工列表NSMutableArray
创buildNSMutableDictionary
一个例子:
NSMutableArray *emloyees = [[NSMutableArray alloc]initWithObjects:@"saman",@"Ruchira",@"Rukshan",@"ishan",@"Harsha",@"Ghihan",@"Lakmali",@"Dasuni", nil]; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; for (NSString *word in emloyees) { NSString *firstLetter = [[word substringToIndex:1] uppercaseString]; letterList = [dict objectForKey:firstLetter]; if (!letterList) { letterList = [NSMutableArray array]; [dict setObject:letterList forKey:firstLetter]; } [letterList addObject:word];} NSLog(@"dic %@",dict);
- (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array { NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init]; [array enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop){ NSNumber *index = [NSNumber numberWithInteger:idx]; [mutableDictionary setObject:obj forKey:index]; }]; NSDictionary *result = [NSDictionary.alloc initWithDictionary:mutableDictionary]; return result; }
我已经创build了一个简单的库,称为Linq到ObjectiveC ,这是一个方法的集合,使得这种问题更容易解决。 在你的情况下,你需要Linq-to-ObjectiveC的toDictionary方法,其中你的'int'字段是通过一个关键select器指定的:
NSDictionary* dictionary = [sourceArray toDictionaryWithKeySelector:^id(id item) { return [item intField]; }];
这将返回一个字典,其中的键由源数组中的intField
给出。