如何从NSDictionary获取密钥和价值?

时间:2014-12-14 15:07:58

标签: objective-c

这是我的代码,但它不会记录任何键或值

NSDictionary *d = @{
      @"foo": @"bar",
      @"abc": @"def"
    };

    for (NSString* key in d.allKeys) {
        NSString *value = (NSString *)[d objectForKey:key];
        NSLog(@"areacodedisplay: value = %@",value);
        NSLog(@"areacodedisplay: key = %@",key);
    }

如何从NSDictionary获取密钥和值?

2 个答案:

答案 0 :(得分:2)

请检查一下,它的工作正常

代码:

 NSDictionary *d = @{
                        @"foo": @"bar"
                        };

    for (NSString* key in d.allKeys)
    {
        NSString *value = [d objectForKey:key];
        NSLog(@" value = %@",value);
        NSLog(@"key = %@",key);
    }

谢谢sushil

答案 1 :(得分:-1)

看这里:for each loop in objective c for accessing NSMutable dictionary

for (NSString* key in d) {
    id value = [d objectForKey:key];
    NSLog(@"areacodedisplay: value = %@",value);
    NSLog(@"areacodedisplay: key = %@",key);
}