我已经看到各种各样的答案,帮助解释在一对多关系中添加记录,但我似乎无法为我的代码制作它的头部或尾部。
我有一段关系 - 国家有很多县
我正在向已经存在的国家添加县,但似乎无法获得将县添加到国家/地区关系的方法......
我的代码是:
@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'
关系设置为
国家
县
答案 0 :(得分:1)
尝试更改
Country * newCounty;
到
County * newCounty;
)