字典中的绘图点

Plot Points from dictionary

我正在从服务器获取纬度、经度和许多其他字段。

我将数据库中的每一行放在数组中的单独字典中。

我试图从纬度和经度绘制出一个点,但是当我尝试按键访问纬度时,出现编译器错误 No visible @interface for NSDictionary...

服务器响应

[{"id":1,"user_id":0,"latitude":"42.2367","longitude":"-71.11332","status":"active","responded_at":null,"created_at":"2015-04-13T12:52:51.144Z","updated_at":"2015-04-13T12:52:51.161Z"},{"id":2,"user_id":0,"latitude":"42.23497","longitude":"-71.11238","status":"active","responded_at":null,"created_at":"2015-04-13T12:57:03.000Z","updated_at":"2015-04-13T12:57:03.002Z"},{"id":3,"user_id":0,"latitude":"42.24222","longitude":"-71.11536","status":"active","responded_at":null,"created_at":"2015-04-13T12:57:49.012Z","updated_at":"2015-04-13T12:57:49.014Z"},{"id":4,"user_id":0,"latitude":"42.24194","longitude":"-71.11556","status":"active","responded_at":null,"created_at":"2015-04-13T13:03:10.710Z","updated_at":"2015-04-13T13:03:10.713Z"},{"id":5,"user_id":0,"latitude":"42.23493","longitude":"-71.11244","status":"active","responded_at":null,"created_at":"2015-04-13T13:05:39.713Z","updated_at":"2015-04-13T13:05:39.716Z"},{"id":6,"user_id":0,"latitude":"42.23598","longitude":"-71.11467","status":"active","responded_at":null,"created_at":"2015-04-13T13:08:12.983Z","updated_at":"2015-04-13T13:08:12.986Z"},{"id":7,"user_id":0,"latitude":"42.23598","longitude":"-71.11467","status":"active","responded_at":null,"created_at":"2015-04-13T13:08:38.115Z","updated_at":"2015-04-13T13:08:38.118Z"},{"id":8,"user_id":0,"latitude":"42.23794","longitude":"-71.11471","status":"active","responded_at":null,"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];

    }
}

服务器响应(就像你在问题中所说的那样)是一个字典数组。

所以 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];
}