![](/img/trans.png)
[英]EXC_BAD_ACCESS while doing NSDictionary objectForKey - but the object is there
[英]How to access data in a NSDictionary with objectForKey
我正在尝试访问字典中的数据。 当它是一个字符串时,我可以访问数据:
cell.textLabel.text = [[item objectForKey:@"merchant"]objectForKey:@"name"];
但不确定NSNumber的语法:
NSNumber* latFromTable = [[displayItems objectAtIndex:indexPath.row]
[objectForKey:@"coords"]objectForKey:@"lat"];
我知道我在上面的代码中使用NSStrings,将无法正常工作。 我如何将其更改为与NSNumber一起使用?
字典中的数据如下所示。 objectForKey是“ coords”。 我想访问项目1和2(经纬度)
"coords": {
"lat": 52.5032,
"lng": 13.3445
谢谢你的帮助。
以下代码的问题是括号不匹配:
NSNumber* latFromTable = [[displayItems objectAtIndex:indexPath.row]
[objectForKey:@"coords"]objectForKey:@"lat"];
因此,将其更改为:
float latFromTable = (float)[[[displayItems objectAtIndex:indexPath.row]
objectForKey:@"coords"]objectForKey:@"lat"];
NSNumber *value = [NSNumber numberWithFloat:latFromTable];
像这样尝试:
float latFromTable = [[[displayItems objectForIndex:indexPath.row]
[objectForKey:@"coords"]objectForKey:@"lat"] floatValue];
字典中的键值对称为条目。 每个条目都包含一个代表键的对象和另一个代表键值的对象。
NSInteger不是对象(它只是一个int值)。
所以你做不到
--- ADDED如schubam所建议的,您可以接收NSString并调用floatValue。
--- ADDED2我已经更准确地阅读了日志。 您有一个具有经度和纬度属性的对象。 因此,它不是第二级字典。 尝试接收接口名称并获取这些接口的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.