0

問題

MKMapViewに問題があります。初期化していくつかの注釈を追加しようとすると、アプリがクラッシュし、SIGABRTが発生し、「キャッチされない例外'NSInvalidArgumentException'、理由:'-[__ NSCFSet addObject: ]:nil'"を挿入しようとしています。NSLogsとコードをいじってみましたが、[mapView addAnnotation:myAnnotation]を呼び出すたびに発生することがわかりました。両方のアノテーションを個別に試しましたが、アプリがクラッシュします。

コード

これが私のMKMapViewに使用しているコードです

IBOutlet MKMapView *mapView; //these are in interface
DisplayMap *thing1; //yes, I have @properties too, and I synthesize them
DisplayMap *thing2;

-(void) initMap //called in viewDidLoad after [super viewDidLoad]
{
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
mapView.showsUserLocation = YES;
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 0;
region.center.longitude = 0;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES]; 
[mapView setDelegate:self];

thing1.title = @"thing1";
thing1.subtitle = @"is here"; 
thing1.coordinate = region.center; 

thing2.title = @"thing2";
thing2.subtitle = @"is somewhere"; 
CLLocationCoordinate2D thing2Coord = {0.005,0.005};
thing2.coordinate = thing2Coord;
[mapView addAnnotation:thing1];
[mapView addAnnotation:thing2];
}

//and my DisplayMap code
//the .h
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>

@interface DisplayMap : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate; 
NSString *title; 
NSString *subtitle;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle;

@end
//the .m
#import "DisplayMap.h"

@implementation DisplayMap
@synthesize coordinate,title,subtitle;

-(void)dealloc{
[title release];
[subtitle release];
[super dealloc];
}

@end

理論

initMapを分離して、残りのアプリが読み込まれる前に呼び出されるようにしました。後で他のプロセスを実行していますが、まだ初期化されていないため、この時点ではアクティブになっていないはずです。この問題は、MKMapView自体の初期化、またはDisplaymapプロパティに関係している可能性があると思います。Xcodeはそれが何であれそれを検出することができず、私が得ているエラーが何を意味するのか本当にわかりません。

4

1 に答える 1

2

thing1あなたのコードからは、あなたが今までにインスタンス化しているようには見えませんthing2

init関数の最初にこれらの行を追加してみてください。

 thing1 = [[DisplayMap alloc] init]; 
 thing2 = [[DisplayMap alloc] init];

これらのメンバーがプロパティであるからといって、それらが自動初期化されることを意味するわけではありません。

于 2012-07-06T14:09:46.907 に答える