我正在构建一系列 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];
}
}
不知道为什么会这样。