I need to be able to display an object value no matter what the key is. Or display an object with the objects key.
NSMutableArray *array = [NSMutableArray array];
NSMutableDictionary *dict= [[NSMutableDictionary alloc] init];
[dict setObject:[NSNumber numberWithInt:10] forKey:@"key"];
[array addObject:dict];
NSLog(@"Key: %@", [[array objectAtIndex:0] key]);
NSLog(@"Value: %@", [[array objectAtIndex:0] valueForKey:[[array objectAtIndex:0] key]]);
//Both logs "[__NSDictionaryM key]: unrecognized selector sent to instance"
Why doesn't this work? And how should I do it?
NOTE: In this example the keyValue
is "key", BUT I don't know the keyValue
in the original code..
NSMutableArray *array = [NSMutableArray array];
NSMutableDictionary *dict= [[NSMutableDictionary alloc] init];
[dict setObject:[NSNumber numberWithInt:10] forKey:@"key"];
[array addObject:dict];
Try
NSDictionary *dict = [array objectAtIndex:0];
NSArray *values = [dict allValues];
NSArray *keys = [dict allKeys];
and log by
NSLog(@"Keys: %@", keys);
NSLog(@"Values: %@", values);
also
for (id key in keys) {
NSLog(@"Value: %@ for Key: %@", [dict objectForKey:key], key);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다