0

我正在构建一系列 MKPolygons 并将它们存储在一个 NSValues 数组中:

for (NSDictionary* country in countries) {
    NSMutableArray* polygons = [[NSMutableArray alloc] init];

    for (NSArray* polygon in [country objectForKey:@"polygons"]) {
        CLLocationCoordinate2D polygonCoords[polygon.count];

        int i;
        for (i = 0; i < polygon.count; i++) {
            NSValue* coords = [polygon objectAtIndex:i];
            CLLocationCoordinate2D stored_coords;
            [coords getValue:&stored_coords];
            polygonCoords[i] = stored_coords;
        }

        MKPolygon* poly = [MKPolygon polygonWithCoordinates:polygonCoords count:polygon.count];
        [polygons addObject:[NSValue valueWithBytes:&poly objCType:@encode(MKPolygon)]];
        [chillPillMap addOverlay:poly];
    }
    [country setValue:polygons forKey:@"polygon_objects"];
}

但是,当我稍后尝试访问它们时,我得到了两个或三个,并且发生了 EXC_BAD_ACCESS:

for (NSDictionary* country in countries) {
   NSArray* polygon_objects = [country objectForKey:@"polygon_objects"];
   int i;
   for (i = 0; i < polygon_objects.count; i++) {
        MKPolygon* saved_poly = [MKPolygon alloc];
        [[polygon_objects objectAtIndex:i] getValue:&saved_poly];
   }
}

不知道为什么会这样。

4

1 回答 1

1

MKPolygon 是一个 Objective-C 对象。您可以将其放入数组中,而无需将其转换为 NSValue。此外,您告诉 NSValue 获取字节的值,但您将指针的地址传递给它。坏消息。

为什么不只是...

[polygons addObject:poly]

然后...

MKPolygon *saved_poly = [polygon_objects objectAtIndex:i];
于 2012-04-24T23:58:50.880 回答