46

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

4

6 に答える 6

77

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

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;
于 2013-10-16T22:20:31.317 に答える
53
- (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];
}
于 2009-09-12T11:02:09.227 に答える
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;
}
于 2009-09-12T12:49:29.417 に答える
1

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);
于 2015-10-29T07:04:18.250 に答える
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;
}
于 2014-03-15T21:42:19.137 に答える
-1

Linq to ObjectiveCと呼ばれる単純なライブラリを作成しました。これは、この種の問題を解決しやすくするメソッドのコレクションです。あなたの場合、「int」フィールドがキーセレクターを介して指定されるLinq-to-ObjectiveC toDictionaryメソッドが必要です。

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

intFieldこれは、キーがソース配列で指定された辞書を返します。

于 2013-03-06T08:36:03.183 に答える