I’m constructing a series of MKPolygons and storing them in an array of 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"];
}
However, when I try to access them later, I get two or three in and a EXC_BAD_ACCESS occurs:
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];
}
}
Not sure why this is.
MKPolygon is an objective-C object. You can put it into the array without converting it to a NSValue. Furthermore, you are telling NSValue to take the value of the bytes, but you are passing it the address of the pointer. Bad news.
Why not just…
then…