목표 :가있는 NSArray 우아한 코드를 이용하여 주어진 NSDictionary와 (S)에 대한 고유 키를 포함 수득NSArray 내에서 고유 한 NSDictionary 키를 찾는보다 우아한 방법이 있습니까?
현재 작업 용액과예 번호 :
NSArray *data = [[NSArray alloc] initWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1], @"a", [NSNumber numberWithInt:2], @"b", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:3], @"b", [NSNumber numberWithInt:4], @"c", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:5], @"a", [NSNumber numberWithInt:6], @"c", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:7], @"b", [NSNumber numberWithInt:8], @"a", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:8], @"c", [NSNumber numberWithInt:9], @"b", nil],
nil];
// create an NSArray of all the dictionary keys within the NSArray *data
NSMutableSet *setKeys = [[NSMutableSet alloc] init];
for (int i=0; i<[data count]; i++) {
[setKeys addObjectsFromArray:[[data objectAtIndex:i] allKeys]];
}
NSArray *arrayKeys = [setKeys allObjects];
NSLog(@"arrayKeys: %@", arrayKeys);
키의 원하는 어레이를 리턴
2012-06-11 16:52:57.351 test.kvc[6497:403] arrayKeys: (
a,
b,
c
)
질문 : 더 우아한 방법이 있나요 접근하고 있니? 확실히 배열을 통해 반복하지 않고도 모든 키를 얻을 수있는 KVC 접근 방식이 있어야합니까? 저는 Apple Developer Documentation을보고 있었고 해결책을 찾을 수 없습니다. 어떤 아이디어? (순수한 성능보다는 코드의 우아함).
NSMutableSet *setKeys = [[NSMutableSet alloc] init];
for (NSDictionary* dict in data) {
for (id key in [dict keyEnumerator]) {
[setKeys addObject:key];
}
}
을하지만 특히 일반적인 작업을 수행하지 않는, 그래서 몇 가지 매우 우아한 방법을 찾을 기대하지 않을 것이다 :
Brilliant. 나는 KVC 방법이 있어야한다는 것을 알았지 만 모든 것을 합칠 수는 없었다. 건배!! –
소원 나는 더 많은 시간이 답변을 upvote 수 있습니다! – Sohan