我已经看到了有关帮助解释在一对多关系中添加记录的各种答案,但我似乎无法为我的代码弄清楚它的头或尾。
我有一段关系 - 国家有很多县
我正在将县添加到已经存在的国家/地区,但似乎无法获得将县添加到国家/地区关系的方法...
我的代码是:
@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
国家/地区.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
在我的 Controller 中:
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 进行查找
编辑
错误 在“国家/地区”类型的对象上找不到属性“国家/地区”
关系设置为
国家
- 关系:县
- 目的地:县
- 逆向:国家
- 对多关系:已选中
县
- 关系:国家
- 目的地:国家
- 逆:县
- 对多关系:未选中
最佳答案
尝试改变
Country * newCounty;
至
County * newCounty;
;)
关于iphone - 将一个对象插入多个关系 - IOS Core Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9880366/