web-dev-qa-db-ja.com

NSArrayをNSDictionaryに変換

配列オブジェクトのintフィールドをNSArrayのキーとして使用して、NSDictionaryNSDictionaryに変換するにはどうすればよいですか?

45
johnl
- (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];
}
53
Alex Reynolds

この魔法を試してください:

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;
76
malhal

これにより、NSArrayにカテゴリ拡張が追加されます。必要なC99モード(最近はデフォルトですが、念のため)。

.hどこかにあるファイル#imported by all ..

@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;
}
9
johne

これは、従業員リスト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);
2
damith
- (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;
}
1
AcornHacks