1

コードのスニペットに問題があります。addObjectメソッドを使用してCLLocationCoordinate2DのインスタンスをNSMutable配列に追加しようとしていますが、行が実行されるたびにアプリがクラッシュします。このコードに明らかな問題はありますか?

クラッシュは次の行にあります:

[points addObject:(id)new_coordinate];

Polygon.m:

#import "Polygon.h"

@implementation Polygon
@synthesize points;

- (id)init {
    self = [super init];
    if(self) {
        points = [[NSMutableArray alloc] init];
    }
    return self;
}


-(void)addPointLatitude:(double)latitude Longitude:(double)longitude {
    NSLog(@"Adding Coordinate: [%f, %f] %d", latitude, longitude, [points count]);
    CLLocationCoordinate2D* new_coordinate = malloc(sizeof(CLLocationCoordinate2D));
    new_coordinate->latitude = latitude;
    new_coordinate->longitude = longitude;
    [points addObject:(id)new_coordinate];
    NSLog(@"%d", [points count]);
}


-(bool)pointInPolygon:(CLLocationCoordinate2D*) p {
    return true;
}


-(CLLocationCoordinate2D*) getNEBounds {
    ...
}

-(CLLocationCoordinate2D*) getSWBounds {
    ...
}


-(void) dealloc {
    for(int count = 0; count < [points count]; count++) {
        free([points objectAtIndex:count]);
    }

    [points release];
    [super dealloc];
}

@end
4

3 に答える 3

6

NSObjectから派生したオブジェクトのみを配列に追加できます。適切なオブジェクト(NSDataなど)内にデータをカプセル化する必要があります。

例えば:

CLLocationCoordinate2D* new_coordinate = malloc(sizeof(CLLocationCoordinate2D));
    new_coordinate->latitude = latitude;
    new_coordinate->longitude = longitude;
    [points addObject:[NSData dataWithBytes:(void *)new_coordinate length:sizeof(CLLocationCoordinate2D)]];
    free(new_coordinate);

オブジェクトの取得:

CLLocationCoordinate2D* c = (CLLocationCoordinate2D*) [[points objectAtIndex:0] bytes];
于 2009-09-08T09:25:40.473 に答える
2

これを行う適切な方法は、データをa内にカプセル化することです。これは、NSValue特にCタイプをNSArraysおよびその他のコレクションに配置するためのものです。

于 2009-09-08T12:32:33.370 に答える
0

カスタムコールバックで関数を使用して、CFArrayCreateMutable保持/解放しない可変配列を作成できます。

于 2009-09-08T12:25:38.720 に答える