2011-08-28 3 views
1

NSArrays 및 NDictionary에 대한 빠른 질문.NSArray 내 NSArrays

NSArray에는 NSDictionary가 포함되어 있습니다. NSDictionary에는 날짜와 문자열이 들어 있습니다.

내가 뭘하고 싶은데요 날짜 키와 그 날짜에있는 문자열의 값 배열을 NSDictionary로 끝납니다.

내가이 일을위한 합리적인 동기 부여가 표시되지 않기 때문에이

답변

2
NSMutableDictionary *result = [NSMutableDictionary dictionary]; 
for (NSDictionary *dict in array) { 
    NSDate *date = [dict objectForKey:@"dateKey"]; 
    NSString *string = [dict objectForKey:@"stringKey"]; 
    NSMutableArray *stringsWithDate = [result objectForKey:date]; 
    if (!stringsWithDate) { 
     stringsWithDate = [NSMutableArray array]; 
     [result setObject:stringsWithDate forKey:date]; 
    } 
    [stringsWithDate addObject:string]; 
} 

참고.

+0

NSCalendar * cal = [NSCalendar currentCalendar]를 사용하여 모든 NSDate가 00:00인지 확인했습니다. [cal setTimeZone : [NSTimeZone timeZoneForSecondsFromGMT : 0]]; NSDateComponents * comp = [cal 구성 요소 : (NSMonthCalendarUnit | NSYearCalendarUnit | NSDayCalendarUnit) fromDate : tempDate]; NSDate * dateToAdd = [cal dateFromComponents : comp]; – user913059

+0

해야합니다 [결과 setObject : stringsWithDate forKey : date]; if 문 다음에 오면 완벽하게 작동합니다. 감사합니다. – user913059

+0

실제로 if 문 내부의'[result setObject : ...]'와 함께 작동해야합니다. – omz

2

을 할 수있는 가장 좋은 방법이 될 것입니다 무엇,의 코드 골프를 호출 할 수 있습니다. NSDate는 "달력 날짜는"아니다, 그래서 다른 시간에 같은 일이 당신의 결과를 사전에 뚜렷한 날짜로 간주됩니다

NSMutableArray *dates = [NSMutableArray array]; 
NSMutableArray *strings = [NSMutableArray array]; 

for (NSDictionary *dict in dictArray) { 
    [dates addObject:[dict objectForKey:@"date"]]; 
    [strings addObject:[dict objectForKey:@"string"]]; 
} 

NSArray *datesArray = [[NSArray alloc] initWithArray:dates]; 
NSArray *stringsArray = [[NSArray alloc] initWithArray:strings];