使用NSManagedObject进行注释

时间:2012-10-29 20:02:16

标签: objective-c core-data mapkit

我对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强烈地引用了那些对象,所以我不太确定我是如何“丢失”数据的。有关正在发生的事情的任何提示?或者我采取了错误的做法?

1 个答案:

答案 0 :(得分:0)

坐标访问器更简单:

- (CLLocationCoordinate2D)coordinate{
    return CLLocationCoordinate2DMake(self.latitude, self.longitude);
}
相关问题