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;

プロトコルの場合、コールアウトが表示されます。それらの1つは次のようになります。

- (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;
}

ただし、これを行うと、すべての値が null になります。この状況でイニシャライザを記述する適切な方法は何ですか? ありがとう!

4

1 に答える 1

0
于 2012-06-27T10:03:43.440 に答える