-[__NSArrayM objectForKey:]: unrecognized selector sent to instance ...
Terminating app due to uncaught exception 'NSInvalidArgumentException'...
JSON 結構中 result 對應的是一個**數組**([ ... ]),而非字典。代碼中直接將其轉為 NSDictionary *book 並嘗試 objectForKey:@"title",導致崩潰。
NSDictionary *jsonResults = (NSDictionary *)[json objectWithString:output error:&jsonError];
NSArray *resultArray = [jsonResults objectForKey:@"result"]; // 這是一個數組
if (resultArray.count > 0) {
NSDictionary *firstBook = [resultArray firstObject]; // 取第一個元素
NSString *title = [firstBook objectForKey:@"title"]; // 現在可以正常取值
NSLog(@"Title: %@", title); // 輸出: Yeah
}
{} → NSDictionary[] → NSArray使用 NSLog(@"%@", jsonResults); 打印完整結構,或在 Xcode 中設置斷點查看實際類型。