2009-09-12 9 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モードが必要です(最近のデフォルトですが、その場合に備えて)。すべてによって#import EDになることができますどこか.hファイルで

... .mファイルで

@interface NSArray (indexKeyedDictionaryExtension) 
- (NSDictionary *)indexKeyedDictionary 
@end 

..

@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

-2

私はLinq to ObjectiveCという簡単なライブラリを作成しました。このライブラリは、この種の問題を解決しやすくしています。これは、キーがソース配列にintFieldによって与えられている辞書を返し

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

:あなたのケースでは、あなたの "int型フィールドがキーセレクタを介して指定されたLinq-to-ObjectiveC toDictionary方法を、必要とします。

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

これは本当に魔法です!エレガントで本当に便利なソリューション!ちょうど注:おそらく、それは "intField"も含まれているように、[valueForKey:@ "otherField"]レコードだけでなく、レコードを使う方が良いでしょう。とにかくそれは何年ものコーディングの後でさえ私のために新しい... – BootMaker

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); 
関連する問題