我是Core Data的新手,編寫一個小測試應用程序。我想要做的是有一個對象,我可以保存到一個SQLite數據庫。爲Core Data對象創建初始化程序
我的屬性是這樣的:
@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 {
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;
}
但是當我這樣做,我的所有值都爲空。在這種情況下編寫初始化程序的正確方法是什麼?謝謝!
對我來說很有趣的部分是當我使用+類方法創建我的對象時,當我將該pin放置在地圖上時調用viewForAnnotation:方法時,會顯示標註。當我使用 - 實例方法時,由於.name值爲空,所以不會顯示標註。思考?謝謝! – Crystal