从NSArray获取行

时间:2012-11-14 09:13:49

标签: ios json nsarray row

你好我得到一个看起来像这样的json:

  features: (
    {
    attributes =         {
        Gecontroleerd = Ja;
    };
    geometry =         {
        x = "5.968097965285907";
        y = "52.50707112779077";
    };
}
)

从这段代码:

NSDictionary *root = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSArray *data = [root objectForKey:@"features"];
NSLog(@"features: %@", data );

for (NSArray *row in data) {
    NSString *latitude = row[5];
    NSString *longitude = row[7];
    NSString *crimeDescription = @"test";
    NSString *address = @"banaan";

对于纬度,你需要x值5.968097965285907 和经度值y值52.50707112779077

但是我收到了这个错误:

 [__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance   0x14831450
 2012-11-14 10:10:59.000 ArrestPlotter[6330:c07] *** Terminating app due to uncaught  exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x14831450'
 *** First throw call stack:
(0x1860012 0x1655e7e 0x18eb4bd 0x184fbbc 0x184f94e 0x2dbc 0x3b8f 0x1cc1e 0x16696b0 0xfa0035 0x17e3f3f 0x17e396f 0x1806734 0x1805f44 0x1805e1b 0x224a7e3 0x224a668 0x4a765c 0x25bd 0x24e5 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

我需要选择哪一行?

我想唯一的问题是需要更改行号。或许应该有类似的东西:[1] [5]。我不太确定这是如何工作的

2 个答案:

答案 0 :(得分:3)

NSArray *data = [root objectForKey:@"features"];
NSLog(@"features: %@", data );

for (NSDictionary *dic in data) {
  NSDictionary geometry = [dic objectForKey:@"geometry"];
  // Do what you want..
  NSString *myAwesomeX = [geometry objectForKey:@"x"];
  NSString *myAwesomeY = [geometry objectForKey:@"y"];
}

答案 1 :(得分:2)

这里的问题是你试图将一个选择器消息发送到对象行,即内存中的一个 NSDictionary (NSCFDictionary?)对象,你试图像 NSArray的

方法objectAtIndexedSubscript(由行[5]和行[7]调用的底层)存在于NSDictionary中,但在NSArray中不存在。

更改

for (NSArray *row in data) {

通过

for (NSDictionary *row in data) {

此外,您必须更改其中的数据管理,查看日志语句的结果并执行一致。

我希望这会有所帮助!