当[mapView addOverlay:overlayPolygon];枚举时,集合发生了变异.

Des*_*ond 2 iphone xcode ios android-mapview

我厌倦了查看SF的解决方案,但无法找到解决方案.也许我错过了请帮助.

我试图通过循环遍历所有KML来检查用户精确定位多边形.这个应用总是崩溃@这一点:

            [mapView addOverlay:overlayPolygon];

            // zoom the map to the polygon bounds
            [mapView setVisibleMapRect:overlayPolygon.boundingMapRect animated:YES];
Run Code Online (Sandbox Code Playgroud)

问题代码:

//create KML in hidden Mapview
-(void)loadKML:(NSMutableArray *)kmlNameArray
{    
    //dispatch_group_t group = dispatch_group_create();


    //remove polygon and redraw again.
    [NSThread detachNewThreadSelector: @selector(spinEnd) toTarget:self withObject:nil];

    [mapView removeOverlays:mapView.overlays];
    [inUserRangeArray removeAllObjects];
    [inUserRangeArrayObjectIndex removeAllObjects];
    [scrollview removeFromSuperview];
    [pageControl removeFromSuperview];
    [NSThread detachNewThreadSelector: @selector(spinBegin) toTarget:self withObject:nil];

    NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
    NSString *docDirectory = [sysPaths objectAtIndex:0];   


    for (int e=0; e<[kmlNameArray count]; e++) 
    {        
        //NSString *kmlNameStr = [kmlNameArray objectAtIndex:e];
        Frog *kmlID = [self.fs objectAtIndex:e];
        self.kmlID = [NSString stringWithFormat:@"%i",kmlID.fID];
        self.kmlIDObjectIndex = [NSString stringWithFormat:@"%i",e];

        NSLog(@"asasas %@",kmlIDObjectIndex);

        //NSLog(@"KML items %@", kmlNameStr);          
        //NSLog(@"KML ID %@", kmlID);
        //NSLog(@"KML file Path %@",[NSString stringWithFormat:@"%@/data/%@/%@", docDirectory,self.kmlID,[kmlNameArray objectAtIndex:e]]);


        SimpleKML *kml = [SimpleKML KMLWithContentsOfFile:[NSString stringWithFormat:@"%@/data/%@/%@", docDirectory,self.kmlID,[kmlNameArray objectAtIndex:e]]error:NULL];

        // look for a document feature in it per the KML spec


        //        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

        if (kml.feature && [kml.feature isKindOfClass:[SimpleKMLDocument class]])
        {// see if the document has features of its own
            for (SimpleKMLFeature *feature in ((SimpleKMLContainer *)kml.feature).features)
            {// otherwise, see if we have any placemark features with a polygon
                if ([feature isKindOfClass:[SimpleKMLPlacemark class]] && ((SimpleKMLPlacemark *)feature).polygon)
                {
                    SimpleKMLPolygon *polygon = (SimpleKMLPolygon *)((SimpleKMLPlacemark *)feature).polygon;
                    SimpleKMLLinearRing *outerRing = polygon.outerBoundary;
                    //points[i], i = number of coordinates
                    CLLocationCoordinate2D points[[outerRing.coordinates count]];
                    NSUInteger i = 0;
                    for (CLLocation *coordinate in outerRing.coordinates)
                    { 
                        points[i++] = coordinate.coordinate;
                    }
                    // create a polygon annotation for it
                    self.overlayPolygon = [MKPolygon polygonWithCoordinates:points count:[outerRing.coordinates count]];

                    //crash here
                    [mapView addOverlay:overlayPolygon];

                    // zoom the map to the polygon bounds
                    [mapView setVisibleMapRect:overlayPolygon.boundingMapRect animated:YES];

                }
            }
        } 
    }
Run Code Online (Sandbox Code Playgroud)

Jor*_*ers 5

在循环遍历数组之前,可以使用元素创建新数组.因此,当原始循环数组发生变异时(由您或其所有者),您循环的数组保持不变.

NSArray *theFeatures = [NSArray arrayWithObjects:((SimpleKMLContainer *)kml.feature).features];
for (SimpleKMLFeature *feature in theFeatures) {

}
Run Code Online (Sandbox Code Playgroud)

因此,如果SimpleKMLContainer直接循环遍历这些功能,您可以创建一个包含这些功能的临时新数组,并循环遍历该数组.

因为您遇到崩溃,所以addOverlay:必须以某种方式循环遍历整个叠加集合.我没有直接在你的代码中看到,所以我假设该features集合以某种方式绑定到地图工具包叠加层.