将NSDictionary中的项添加到NSMutableArray

时间:2011-10-17 13:53:03

标签: iphone objective-c nsmutablearray nsdictionary

我正在尝试使用iPhone MapKit为地图注释实现k-means聚类算法。我有两个实现MKAnnotation的类:各个点的PostLocationAnnotation和点集群的LocationGroupAnnotation。 LocationGroupAnnotation的标题如下:

@interface LocationGroupAnnotation : NSObject <MKAnnotation>
{

    NSMutableArray *_locations;
    CLLocationCoordinate2D _coordinate;

}

@property (nonatomic, retain) NSArray *locations;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

+ (LocationGroupAnnotation *)initWithLocations:(NSArray *)locationArray;
+ (LocationGroupAnnotation *)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
- (id)initWithLocations:(NSArray *)locationArray;
- (id)initWithCoordinate:(CLLocationCoordinate2D)startCoordinate;
- (void)addAnnotation:(PostLocationAnnotation *)annotation;
- (NSUInteger)locationCount;

@end

在我的地图视图控制器中,我有一个方法将所有PostLocationAnnotations添加到LocationGroupAnnotations,方法是创建k个LocationGroupAnnotations(使用initWithCoordinate方法),每个方法都有一个从注释中随机选择的位置,并将PostLocationAnnotations添加到最近的LocationGroupAnnotations。为此,我使用了一个NSMutableArray的NSMutableDictionary对象,就像这样('means'是一个在此之前填充的LocationGroupAnnotations的NSMutableArray):

// find nearest cluster to each point
NSMutableArray *distances = [[NSMutableArray alloc] initWithCapacity:[[ffMapView annotations] count]];

for (id <MKAnnotation> point in [ffMapView annotations])
{
    if ([point isKindOfClass:[PostLocationAnnotation class]])
    {
        NSMutableDictionary *distanceDict = [[NSMutableDictionary alloc] initWithCapacity:2];

        [distanceDict setObject:point forKey:@"location"];

        double minDistance = 0;
        LocationGroupAnnotation *closestMean = nil;

        for (id <MKAnnotation> meanPoint in means)
        {
            if ([meanPoint isKindOfClass:[LocationGroupAnnotation class]])
            {
                if (closestMean)
                {
                    if ([ClusteringTools getDistanceBetweenPoint:[point coordinate] and:[meanPoint coordinate]] < minDistance)
                    {
                        closestMean = meanPoint;
                        minDistance = [ClusteringTools getDistanceBetweenPoint:[point coordinate] and:[meanPoint coordinate]];
                    }
                } else {
                    closestMean = meanPoint;
                    minDistance = [ClusteringTools getDistanceBetweenPoint:[point coordinate] and:[meanPoint coordinate]];
                }
            }
        }

        [distanceDict setObject:closestMean forKey:@"closestMean"];

        [distances addObject:distanceDict];

        [distanceDict release];
    }
}

// add annotations to clusters
for (NSDictionary *entry in distances)
{
    [(LocationGroupAnnotation *)[entry objectForKey:@"closestMean"] addAnnotation:[entry objectForKey:@"location"]];
}

我遇到的问题是在此之后输出每个LocationGroupAnnotation的locationCount时,每个都得到0。每次添加注释时都会有一个日志输出,就像这样(在LocationGroupAnnotation中):

- (void)addAnnotation:(PostLocationAnnotation *)annotation
{
    [_locations addObject:annotation];
    NSLog(@"Added annotation at (%f,%f) to cluster %@", 
        [annotation coordinate].latitude,
        [annotation coordinate].longitude,
        [self description]);
}

......看起来所有东西都应该添加到应该的位置,从内存地址来判断。那是怎么回事?

1 个答案:

答案 0 :(得分:2)

您是否在任何地方初始化_locations?看起来你可以愉快地将对象添加到nil,这不会导致任何错误。

您尚未显示代码,但您还有locations属性NSArray和实例变量_locations,这是NSMutableArray,您在做什么在背景中有什么聪明的东西可能搞乱了吗?你是如何将这两个属性连接在一起的?