我从服务器获取纬度,经度和许多其他字段。
我将数据库中的每一行放在数组中的单独字典中。
我试图从纬度和经度中绘制出一个点,但是当我尝试按键访问纬度时,我得到编译器错误No visible @interface for NSDictionary...
。
服务器响应
[{" ID":1," USER_ID":0,"纬度":" 42.2367""经度":" -71.11332""状态":"有源"" responded_at":空," created_at& #34;:" 2015-04-13T12:52:51.144Z""的updated_at":" 2015-04-13T12:52:51.161Z"}, {" ID":2" USER_ID":0,"纬度":" 42.23497""经度" :" -71.11238""状态":"有源"" responded_at":空," created_at&#34 ;: " 2015-04-13T12:57:03.000Z""的updated_at":" 2015-04-13T12:57:03.002Z"},{&#34 ; ID":3," USER_ID":0,"纬度":" 42.24222""经度":&#34 ; -71.11536""状态":"有源"" responded_at":空," created_at":" 2015-04-13T12:57:49.012Z""的updated_at":" 2015-04-13T12:57:49.014Z"},{" ID&#34 :4" USER_ID":0,"纬度":" 42.24194""经度":" -71.11556& #34;"状态" :"有源"" responded_at":空," created_at":" 2015-04-13T13:03:10.710Z"&# 34;的updated_at":" 2015-04-13T13:03:10.713Z"},{" ID":5," USER_ID":0,& #34;纬度":" 42.23493""经度":" -71.11244""状态":&#34 ;有源"" responded_at":空," created_at":" 2015-04-13T13:05:39.713Z""的updated_at&# 34;:" 2015-04-13T13:05:39.716Z"},{" ID":6," USER_ID":0,"纬度和#34;:" 42.23598""经度":" -71.11467""状态":"有源&#34 ;," responded_at":空," created_at":" 2015-04-13T13:08:12.983Z""的updated_at":& #34; 2015-04-13T13:08:12.986Z"},{" ID":7," USER_ID":0,"纬度&#34 ;: #&34; 42.23598""经度":" -71.11467""状态":"有源"&# 34; responded_at":空," created_at":" 2015-04-13T13:08:38.115Z""的updated_at":" 2015年 - 04-13T13:08:38.118Z"},{" ID":8," USER_ID":0,"纬度":" 42.23794 #&34;,"经度":" -71.11471""状态":"有源"" responded_at&#34 ;:空," created_at":" 2015-04-13T13:10:11.593Z""的updated_at":" 2015-04-13T13: 11:19.467Z"}]
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
NSError *error;
NSMutableDictionary *json = [NSJSONSerialization
JSONObjectWithData:_responseData
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
for (int i = 0; i < json.count; i++) {
double latitude = [[json objectAtIndex:i] objectForKey:@"latitude"];
double longitude = [[[json objectForKey:@"longitude"]objectAtIndex:i] doubleValue];
CLLocationCoordinate2D latlng = CLLocationCoordinate2DMake(latitude, longitude);
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
[point setCoordinate:latlng];
[worldView addAnnotation:point];
}
}
答案 0 :(得分:2)
服务器响应(就像你在问题中所说的那样)是一个字典数组。
因此json
应声明为NSArray
而不是NSMutableDictionary
。
这是导致编译器错误No visible @interface for NSDictionary...
的原因,因为NSMutableDictionary
没有objectAtIndex:
方法。
此外,获取经度的代码是向后的(它将json
视为字典,然后尝试访问结果键值内的数组。)
更正后的代码为:
NSArray *json = [NSJSONSerialization
JSONObjectWithData:_responseData
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
for (int i = 0; i < json.count; i++) {
NSDictionary *pointDictionary = [json objectAtIndex:i];
double latitude = [[pointDictionary objectForKey:@"latitude"] doubleValue];
double longitude = [[pointDictionary objectForKey:@"longitude"] doubleValue];
CLLocationCoordinate2D latlng = CLLocationCoordinate2DMake(latitude, longitude);
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
[point setCoordinate:latlng];
[worldView addAnnotation:point];
}