1

我对 ios 开发非常陌生,我正在尝试从 to 添加一些值NSMutableDictionaryNSMutableArray当我运行代码时出现此错误

2012-05-29 14:09:34.913 iShop[7464:f803]-[__NSCFArray objectForKey:]: 无法识别的选择器发送到实例 0x6e423d0 2012-05-29 14:09:34.915 iShop[7464:f803] * 由于应用程序终止未捕获的异常'NSInvalidArgumentException',原因是: ' - [__ NSCFArray objectForKey:]:无法识别的选择发送到实例0x6e423d0' *第一掷调用堆栈:(0x13cb022 0x155ccd6 0x13cccbd 0x1331ed0 0x1331cb2 0x2bb7 0x13cce42 0x93b9df 0x139f94f 0x1302b43 0x1302424 0x1301d84 0x1301c9b 0x12b47d8 0x12b488a 0x15626 0x28bd 0x2825)终止称为抛出异常(lldb)

下面是我的代码:

-(void) getData:(NSData *) response {
    NSError *error;

    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&error];
    //NSLog(@"%@", json);

    jsonArray = [[NSMutableArray alloc] init];

    jsonArray = [json objectForKey:@"Name"];

    NSLog(@"%@", jsonArray);

}

- (void)viewDidLoad {
    [super viewDidLoad];

    jsonURL = [NSURL URLWithString:@"http://localhost:8888/read_product_list.php"];
    dispatch_async(BgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL:jsonURL];
        [self performSelectorOnMainThread:@selector(getData:) 
                               withObject:data waitUntilDone:YES];
    });
}
4

1 回答 1

2

JSONObjectWithData返回一个类型的对象id。取决于您收到的 json 数据的结构。接收到的数据是 eiherNSDictionaryNSArray。在您的情况下,我猜测顶级对象的类型NSArray不响应objectForKey

于 2012-05-29T12:32:40.767 回答