如何在特定数组中更改NSMutableArray

时间:2013-05-02 15:50:51

标签: ios objective-c nsmutablearray

我有一个NSMutableArray,它拥有一个自定义类的MKMapkit。

MapPoint *placeObject = [[MapPoint alloc] initWithTitle:title subtitle:subtitle coordinate:loc description:description storeImage:storeImage];
[annotationArray addObject:placeObject];

我的例程在没有图像的情况下填充placeObject,因为它们将异步加载并在几秒钟后完成。现在我的问题是:有没有办法改变placeObject内的annotationArray

修改

在这里以更好的方式显示我的问题一些代码部分:

if (annotationArray == nil)
{
    [self setAnnotationArray:[[NSMutableArray alloc] init]];
}
for (NSDictionary *locationDetails in parser.items)
{
    __block NSData *storeImageData;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
    dispatch_async(queue, ^{
        storeImageData = [NSData dataWithContentsOfURL:storeImageURL];

        dispatch_sync(dispatch_get_main_queue(), ^{
            UIImage *storeImageTMP = [UIImage imageWithData:storeImageData];
            counterBlock ++;
            NSLog(@"async: %d", counterBlock);
            [imageArray addObject:storeImageTMP];
            });
        });
MapPoint *placeObject = [[MapPoint alloc] initWithTitle:title subtitle:subtitle coordinate:loc description:description storeImage:storeImage];
[annotationArray addObject:placeObject];
}
[mapView addAnnotations:annotationArray];

完成此操作后:dispatch_queue_t将开始工作。所以如果它满载,我会每2秒检查一次。然后我开始改变annotationArray并刷新我的注释(删除所有 - >添加到地图女巫附加图像)

- (void) checkMap
{
    if (counterBlock == 547)
    {
        for (int i=0; i<=counterBlock; i++)
        {
            MapPoint *point = annotationArray[i];
            point.storeImage = imageArray[i];
        }
        if(timer)
        {
            [timer invalidate];
            timer = nil;
        }
        [mapView removeAnnotations:mapView.annotations];
        [mapView addAnnotations:annotationArray];
    }
}

3 个答案:

答案 0 :(得分:4)

您需要做的就是获取对数组中MapPoint个对象之一的引用。然后,您可以根据需要在对象上设置属性和/或调用方法。

MapPoint *point = annotationArray[x]; // x is whatever index you need
point.image = ... // some image

答案 1 :(得分:0)

您可以使用objectAtIndex方法从可变数组中检索要更改的对象

NSInteger indexofMyObject = 0;
MapPoint *point = [myArray objectAtIndex:indexofMyObject];

答案 2 :(得分:0)

您的MapPoint实例placeObject是一个指针。因此,将其添加到数组不会按值复制placeObject,而是实际保存其地址。从数组中获取对象的地址将允许您操作它。

annotationArray[objectIndex].anything = anything you want