我一直在使用归档,我正在切换到核心数据 - 但是,我不知道如何实现我想要添加到数据库的某个实体。
以下是我如何使用Core Data编写头文件...
@interface SELCase : NSObject <NSCoding>
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *caseKey;
@property (nonatomic) BOOL individual;
@property (nonatomic) float indivAmount;
@property (nonatomic) SELCase *indivType;
@end
每次加载的数据都是名称,caseKey和个人。如果个人是YES,那么这将是它的结束,但如果它不是个人,那么将加载indivAmount和indivType。
问题是我需要与另一个SELCase的关系,我不知道该怎么做。有什么帮助吗?
答案 0 :(得分:1)
只需创建指向同一实体的关系即可。
以下是一个例子:
@property (nonatomic, retain) NSString * goalName;
@property (nonatomic, retain) NSData * goalPicture;
@property (nonatomic, retain) NSNumber * indexOrder;
@property (nonatomic, retain) NSNumber * wordPicture;
@property (nonatomic, retain) NSSet *item;
@property (nonatomic, retain) Goal *parent;
@property (nonatomic, retain) NSSet *subGoals;
@property (nonatomic, retain) User *user;
@property (nonatomic, retain) NSSet *video;
@property (nonatomic, retain) NSSet *stats;
@end
@interface Goal (CoreDataGeneratedAccessors)
- (void)addItemObject:(Item *)value;
- (void)removeItemObject:(Item *)value;
- (void)addItem:(NSSet *)values;
- (void)removeItem:(NSSet *)values;
- (void)addSubGoalsObject:(Goal *)value;
- (void)removeSubGoalsObject:(Goal *)value;
- (void)addSubGoals:(NSSet *)values;
- (void)removeSubGoals:(NSSet *)values;
- (void)addVideoObject:(Video *)value;
- (void)removeVideoObject:(Video *)value;
- (void)addVideo:(NSSet *)values;
- (void)removeVideo:(NSSet *)values;
- (void)addStatsObject:(Stats *)value;
- (void)removeStatsObject:(Stats *)value;
- (void)addStats:(NSSet *)values;
- (void)removeStats:(NSSet *)values;
在这种情况下,子目标是由相同类型的实体组成的集合。
在你的模型中,你只会建立一个指向同一实体的关系(一个/一个,一个/多个,无论如何)。