核心数据-实现一对一的自定义关系 [英] Core data - implementing a custom to one relationship
问题描述
我不确定这是否是一个愚蠢的问题:
我有一个托管对象A,而托管对象A与表B有多对一的关系。 / p>
我通过执行以下操作将关系A设置为B
.Bobject = acopyOfB;
哪个还可以。
我要为该关系编写自定义设置器。这样一来,我就可以根据B设置的值在A中设置一些只读属性。
所以我想填补空白:
-(void)setBobject:(Bobject)theValue {
'差距'
}
我很高兴能够设置自定义值,但是可以
您将需要做类似的事情
-(void)setBObject:(BObject *)theValue {
[self willChangeValueForKey:@ bObject];
[self setPrimitiveBObject:theValue];
[self didChangeValueForKey:@ bObject];
//做其他您需要在这里做的事情
}
为避免编译器警告,您可以在 @implementation
指令之前声明以下内容(但要在 .m
文件中) ):
@interface MyManagedObjectSubClass(PrimitiveAccessors)
@property(非原子,保留)BObject * nativeBObject ;
@end
I am not sure if this is a dumb question:
I have an managed object A and managed object A has a many to one relationship with table B.
I set the relationship A to B by doing something like
Aobject.Bobject=acopyOfB;
Which is fine.
I want to write a custom setter for that relationship. This is so I can setup some read only properties in A, based on the value that B gets set to.
So I want to fill in the gap:
-(void) setBobject:(Bobject)theValue{
'the gap'
}
I am happy with being able to set my custom values, but can't find anywhere the correct syntax for setting the relationship within core data.
You will need to do something like
- (void) setBObject:(BObject*)theValue {
[self willChangeValueForKey:@"bObject"];
[self setPrimitiveBObject:theValue];
[self didChangeValueForKey:@"bObject"];
// Do whatever else you need to do here
}
To avoid compiler warnings, you can declare the following before the @implementation
directive (but in the .m
file):
@interface MyManagedObjectSubClass (PrimitiveAccessors)
@property (nonatomic, retain) BObject* primitiveBObject;
@end
这篇关于核心数据-实现一对一的自定义关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!