2009-09-12 99 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);