我对Objective-C和iOS比较新,所以我使用NSManagedObject
子类作为MKAnnotation
遇到了问题。我执行获取请求以获取所有“位置”(NSManagedObject
子类和MKAnnotation
),因此我可以填充地图。
在viewDidLoad
MapController
上抓取所有内容都很顺利
- (void)viewDidLoad
{
[super viewDidLoad];
DatabaseHelper *db = [DatabaseHelper newDatabaseHelper]; // does all the fetch requests ahead...
NSArray *places = [[db getPlacesForDeck:@"PlacesDeck"] allObjects];
/*
[places enumerateObjectsUsingBlock:^(Place *place, NSUInteger i, BOOL *stop) {
NSLog(@"Place Number: %d", i);
NSLog(@"Place : %@", place.name);
NSLog(@"Place latitude: %@", place.latitude); ALL FINE HERE
NSLog(@"Place longitude: %@", place.longitude);
}];
*/
self.annotations = places;
[self.mapView addAnnotations:places];
}
NSLog报告正常,但是当地图被绘制为注释标题,并且坐标默认为null,并且(0,0)。
- (NSString *) title{
return self.name; //name is an attribute from NSManagedObject subclass.
}
- (CLLocationCoordinate2D) coordinate{
CLLocationCoordinate2D coord;
coord.latitude = [self.latitude doubleValue]; // Longitude and latitude
coord.longitude = [self.longitude doubleValue]; // also dynamic attributes.
return coord;
}
我的猜测是,当我通过'places'数组时,我以某种方式取消引用每个地方。但是我在某处NSArray
强烈地引用了那些对象,所以我不太确定我是如何“丢失”数据的。有关正在发生的事情的任何提示?或者我采取了错误的做法?
答案 0 :(得分:0)
坐标访问器更简单:
- (CLLocationCoordinate2D)coordinate{
return CLLocationCoordinate2DMake(self.latitude, self.longitude);
}