我想從更高的範圍訪問Objective-C中的JSON。但它不斷崩潰,我不知道爲什麼。爲什麼我的iOS應用使用JSON崩潰?
下面的代碼工作:
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONOnkectWithData: responseData
options:kNilOptions
error:&error];
NSArray* nameAndPlace = [[json objectForKey:@"objects"] objectForKey:@"havens"];
//NSArray* productArray = [[json objectForKey:@"objects"] objectForKey:@"products"];
NSLog(@"FROM ---> %@", nameAndPlace);
for (NSDictionary *mapPointLoop in nameAndPlace) {
NSString * name = [mapPointLoop objectForKey:@"name"];
NSString * longitudeGet = [mapPointLoop objectForKey:@"longitude"];
NSString * latitudeGet = [mapPointLoop objectForKey:@"latitude"];
myAnnotation *annotation1 = [[myAnnotation alloc] init];
CLLocationCoordinate2D coordinate1;
coordinate1.longitude = longitude;
coordinate1.latitude = latitude;
annotation1.coordinate = coordinate1;
annotation1.title = name;
[mainMap addAnnotation:annotation1];
//NSLog(@"FROM ---> %@", mapPointLoop);
}
}
但是,當我使用這個崩潰:
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSArray* nameAndPlace = [json objectForKey:@"objects"];
//NSArray* productArray = [[json objectForKey:@"objects"] objectForKey:@"products"];
NSLog(@"FROM -> %@", nameAndP1ace);
for (NSDictionary *mapPointLoop in nameAndPlace) {
NSString * name = [[mapPointLoop objectForKey:@"havens"] objectForKey:@"name"];
NSNumber * longitudeGet = [mapPointLoop objectForKey:@"longitude"];
NSNumber * latitudeGet = [mapPointLoop objectForKey:@"latitude"];
float latitude = [latitudeGet floatvalue];
float longitude = [longitudeGet floatvalue];
myAnnotation *annotation1 = [[myAnnotation alloc] init];
CLLocationCoordinate2D coordinate1;
coordinate1.longitude = longitude;
coordinate1.latitude = latitude;
annotation1.coordinate = coordinate1
annotation1.title = name;
[mainMap addAnnotation:annotation1];
//NSLog(@"FROM -> %@", mapPointLoop):
}
}
它崩潰與錯誤...
2014年5月20日17:49:57.709 labelAPortTest [5556:60B] *終止應用程序 由於未捕獲的異常「N SInvalidArgumentException」,原因是: ' - [__ NSCFString objectForKey:]:無法識別的選擇發送到實例 0x1172426b0'
發佈實際代碼比發佈代碼圖片更好。文本更易於閱讀,並且可以很容易地將它複製到帶有更正的答案中。 – Caleb
任何包含單詞「無法識別的選擇器」的錯誤消息?展示下。向我們展示實際JSON的副本。 json變量的NSLog就足夠了。 – gnasher729
這兩個代碼塊是不同的......我懷疑這是你得到nameAndPLace導致這個問題的戰爭。什麼是崩潰信息? – CW0007007