将NSArray转换为NSDictionary

时间:2009-09-12 10:49:35

标签: iphone objective-c nsarray nsdictionary

如何将NSArray转换为NSDictionary,使用数组对象的int字段作为NSDictionary的键?

6 个答案:

答案 0 :(得分:74)

试试这个魔法:

NSDictionary *dict = [NSDictionary dictionaryWithObjects:records 
                                   forKeys:[records valueForKey:@"intField"]];

仅供参考,因为这个内置功能,这是可能的:

@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;

答案 1 :(得分:52)

- (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];
}

答案 2 :(得分:9)

这会为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;
}

输出:

{{1}}

答案 3 :(得分:2)

这是从员工列表NSMutableDictionary创建NSMutableArray的示例:

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

答案 4 :(得分:1)

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

答案 5 :(得分:-2)

我创建了一个名为Linq to ObjectiveC的简单库,它是一组方法,使这类问题更容易解决。在您的情况下,您需要Linq-to-ObjectiveC toDictionary方法,其中'int'字段是通过键选择器指定的:

NSDictionary* dictionary = [sourceArray toDictionaryWithKeySelector:^id(id item) {
    return [item intField];
}];

这将返回一个字典,其中的键由源数组中的intField给出。