将一个对象插入多个关系 - IOS Core Data

时间:2012-03-26 21:48:07

标签: iphone ios core-data one-to-many relationship

我已经看到各种各样的答案,帮助解释在一对多关系中添加记录,但我似乎无法为我的代码制作它的头部或尾部。

我有一段关系 - 国家有很多县

我正在向已经存在的国家添加县,但似乎无法获得将县添加到国家/地区关系的方法......

我的代码是:

@class Country, Property;

@interface County : NSManagedObject

@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSDate * last_server_update;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Country *country;
@property (nonatomic, retain) NSSet *properties;
@end

@interface County (CoreDataGeneratedAccessors)

- (void)addPropertiesObject:(Property *)value;
- (void)removePropertiesObject:(Property *)value;
- (void)addProperties:(NSSet *)values;
- (void)removeProperties:(NSSet *)values;

@end

Country.h

@class County, Property;

@interface Country : NSManagedObject

@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSDate * last_server_update;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *properties;
@property (nonatomic, retain) County *counties;
@end

@interface Country (CoreDataGeneratedAccessors)

- (void)addPropertiesObject:(Property *)value;
- (void)removePropertiesObject:(Property *)value;
- (void)addProperties:(NSSet *)values;
- (void)removeProperties:(NSSet *)values;

- (void)addCountiesObject:(County *)value;
- (void)removeCountiesObject:(County *)value;
- (void)addCounties:(NSSet *)values;
- (void)removeCounties:(NSSet *)values;

@end

在我的控制器中:

Country * newCounty;
newCounty = [County createEntity];

// Get Country
Country * relatedCountry = [Country findFirstByAttribute:@"id" withValue:[county objectForKey:@"country_id"]];

newCounty.id = [county objectForKey:@"id"];
newCounty.name = [county objectForKey:@"name"];
newCounty.country = relatedCountry;  // Does not compile

我正在使用MagicalRecord获取发现者btw

修改

错误 在'国家'类型的对象上找不到属性'country'

关系设置为

国家

  • 关系:县
  • 目的地:郡
  • 反向:国家
  • To-Many Relationship:checked

  • 关系:国家
  • 目的地:国家/地区
  • 反向:县
  • To-Many Relationship:unchecked

1 个答案:

答案 0 :(得分:1)

尝试更改

Country * newCounty;

County * newCounty;