English 中文(简体)
将物品从NSDictionary改为NSMutableArray
原标题:Adding items from an NSDictionary to an NSMutableArray

I m trying to make an implementation of a k-means clustering algorithm for map annotations using the iPhone MapKit. I ve got 2 classes which implement MKAnnotation: PostLocationAnnotation for individual points and LocationGroupAnnotation for clusters of points. The header for LocationGroupAnnotation is as follows:

@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

在我的地图观察控制员中,我有办法将所有邮政电传给各地点组别,每个地点都采用从说明中随机挑选的地点,并将邮政电传给最接近的地点组。 为此,我使用一架NSMutable Array的有害物质,如此(是指在座前有人居住的Suntable Array):

// 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"]];
}

我面临的问题是,我在此之后将每个地点组别的科恩混为一谈。 每次添加通知时,我都有一个记录产出(在地点组别):

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

......而且,它想像一切照样,以记忆地址判断。 因此,情况如何?

最佳回答

您是否在任何地方开始使用<条码>。 它认为,在<代码>nil上添加物体,不会造成任何错误。

页: 1 什么是NSArray和一个实例变量_ places? 哪一个是NSMuArray? 你们如何将这些两种财产联系起来?

问题回答

暂无回答




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签