0

我是 Core Data 的新手,正在编写一个小测试应用程序。我想要做的是有一个可以保存到 sqlite 数据库中的对象。

我的属性看起来像:

@property (nonatomic, retain) NSString * address;
@property (nonatomic, retain) NSString * city;
@property (nonatomic, retain) NSNumber * latitude;
@property (nonatomic, retain) NSNumber * longitude;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * notes;
@property (nonatomic, retain) NSString * state;
@property (nonatomic, retain) NSString * street;
@property (nonatomic, retain) NSString * zip;

这些都是 .m 文件中的@dynamic。

我最初是这样做的:

+ (AddressAnnotation *)initWithPlacemark:(CLPlacemark *)placemark inContext:(NSManagedObjectContext *)context {
    AddressAnnotation *anAddress = [NSEntityDescription insertNewObjectForEntityForName:@"AddressAnnotation" inManagedObjectContext:context];

    anAddress.address = placemark.subThoroughfare;
    anAddress.street = placemark.thoroughfare;
    anAddress.city = placemark.locality;
    ....
    return anAddress;
}

但是,我不知道如何覆盖

- (NSString *)title;
- (NSString *)subtitle;

对于协议,以便显示我的标注。其中一个看起来像:

- (NSString *)title {
    if (self.name) {
        return self.name;
    }
    else {
        return @"";
    }
}

字幕非常相似,但具有其他属性。但是,这不起作用,因为这些是针对实例而不是针对类对象的,对吧?

所以我改变了我的初始化程序看起来像:

- (AddressAnnotation *)initWithPlacemark:(CLPlacemark *)placemark inContext:(NSManagedObjectContext *)context {

    AddressAnnotation *anAddress = [NSEntityDescription insertNewObjectForEntityForName:@"AddressAnnotation" inManagedObjectContext:context];
    NSLog(@"placemark: %@", [placemark description]);
    anAddress.address = placemark.subThoroughfare;
    anAddress.street = placemark.thoroughfare;
    ...

    return anAddress;
}

但是,当我这样做时,我所有的值都是空的。在这种情况下编写初始化程序的正确方法是什么?谢谢!

4

1 回答 1

0
于 2012-06-27T10:03:43.440 回答