0

我的目标 C 代码有问题,我收到这些错误。

 2013-01-31 16:22:53.611 iSA-MP[541:c07] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x7465d70
2013-01-31 16:22:53.614 iSA-MP[541:c07] *** Terminating app due to uncaught exception               'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x7465d70'
*** First throw call stack:
(0x1c8c012 0x10c9e7e 0x1d174bd 0x1c7bbbc 0x1c7b94e 0x2163 0xf2817 0xf2882 0x41a25 0x41dbf             0x41f55 0x4af67 0xefcc 0xffab 0x21315 0x2224b 0x13cf8 0x1be7df9 0x1be7ad0 0x1c01bf5 0x1c01962 0x1c32bb6 0x1c31f44 0x1c31e1b 0xf7da 0x1165c 0x1dbd 0x1ce5) 
libc++abi.dylib: terminate called throwing an exception

使用此代码:

  NSString *urlString = [NSString stringWithFormat:@"http://192.168.0.125:100/isa-mp/iSA-MP.php?serverIP=192.168.0.125&serverPort=7777"];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSMutableArray *array = (NSMutableArray*)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

NSLog(@"%@", [array objectAtIndex:0]);
4

2 回答 2

3

您将返回一个字典,如果您不知道收到的数据的内容,则 NSLog[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]并检查您想要成为数组的项目的键。

你可能想做这样的事情:

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *array = dict[@"someKeyForArray"];
于 2013-01-31T15:27:19.243 回答
1

问题是您在 NSDictionary 上调用 objectAtIndex:...

看起来问题出在您正在解析的 JSON 中,JSON 将被格式化,以便解析它返回一个 NSDictionary。即使您正在投射到一个数组,它仍然是一个在幕后的字典。

于 2013-01-31T15:27:15.437 回答