我第一次在 Objective-C 中试验 JSON。
这是我的终端应用程序的代码:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://itunes.apple.com/search?term=harry&country=us"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSArray *publicTimeline = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
NSDictionary *list;
for(int i=0; i<[publicTimeline count];i++)
{
list = [publicTimeline objectAtIndex:i];
NSLog(@"Statuses: %@", [list objectForKey:@"trackName"]);
}
}
return 0;
}
这是我得到的错误:
2013-09-11 20:58:55.524 Tweets[7291:303] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x100391d60
2013-09-11 20:58:55.526 Tweets[7291:303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x100391d60'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff91688b06 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8bf8a3f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff9171f40a -[NSObject(NSObject) doesNotRecognizeSelector:] + 186
3 CoreFoundation 0x00007fff9167702e ___forwarding___ + 414
4 CoreFoundation 0x00007fff91676e18 _CF_forwarding_prep_0 + 232
5 Tweets 0x0000000100000d1a main + 378
6 libdyld.dylib 0x00007fff908e67e1 start + 0
)
libc++abi.dylib: terminate called throwing an exception
怎么了?我完美地遵循了一个教程,但我是唯一一个得到错误的人。