2009-09-12 121 views

回答

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

这会给NSArray增加一个类别扩展名。需要C99模式(这是目前的默认模式,但以防万一)。

.h文件的地方,可以是#import ED由所有..

@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

我喜欢使用类别,所以我会优先考虑这个解决方案,而不是使用简单的函数,因为它直接在对象本身上调用它,就好像它总是知道该方法一样。 – Woofy 2009-09-19 12:10:05

-2

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

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

这将返回它的键由intField源数组中给出的字典。

71

试试这个神奇:

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

FYI这是可能的,因为该内置功能:

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

这真是一种神奇!优雅和真正方便的解决方案!请注意:也许最好使用[记录valueForKey:@“otherField”]不只是记录,因为它也会包含“intField”。无论如何,即使经过多年的编码,它对我来说都是新的... – BootMaker 2018-01-19 16:02:39

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

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

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