【问题标题】:Adding items from an NSDictionary to an NSMutableArray将 NSDictionary 中的项目添加到 NSMutableArray
【发布时间】:2011-10-17 13:53:03
【问题描述】:

我正在尝试使用 iPhone MapKit 实现用于地图注释的 k-means 聚类算法。我有 2 个实现 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。为此,我使用 NSMutableDictionary 对象的 NSMutableArray,如下所示('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]);
}

...从内存地址来看,似乎所有内容都被添加到了应有的位置。那到底是怎么回事?

【问题讨论】:

    标签: iphone objective-c nsmutablearray nsdictionary


    【解决方案1】:

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

    您还没有显示代码,但您还有一个属性 locations 是一个 NSArray 和一个实例变量 _locations 这是一个 NSMutableArray,你在后台做了什么聪明的事情吗?可能把事情搞砸了?您如何将这两个属性链接在一起?

    【讨论】:

    • 宾果游戏。未初始化 _locations!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多