문제

어떻게 변환 할 수 있습니까? NSArray an NSDictionary, 배열 객체의 int 필드를 NSDictionary?

도움이 되었습니까?

해결책

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

다른 팁

이 마법을 시도하십시오 :

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;

이것은 카테고리 확장을 추가합니다 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;
}

이것은 a를 만드는 예입니다 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);
- (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;
}

나는 간단한 라이브러리를 만들었습니다 LINQ에서 Objectivec, 이는 이러한 종류의 문제를 훨씬 쉽게 해결할 수있는 방법의 모음입니다. 당신의 경우 당신은 필요합니다 LINQ-to-Objectivec Todictionary 'int'필드가 키 선택기를 통해 지정되는 방법 :

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

이것은 키가 주어진 사전을 반환합니다. intField 소스 배열에서.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top