且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以覆盖NSManagedObject子类中@dynamic属性的getter和setter?

更新时间:2022-11-22 17:34:36

Never call [super valueForKey:..] in a NSManagedObject subclass! (unless you implemented them in a superclass yourself) Instead use the primitive accessor methods.

Sample getter/setter implementation from ADC:

@interface Department : NSManagedObject
@property(nonatomic, strong) NSString *name;
@end

@interface Department (PrimitiveAccessors)
- (NSString *)primitiveName;
- (void)setPrimitiveName:(NSString *)newName;
@end


@implementation Department

@dynamic name;

- (NSString *)name
{
    [self willAccessValueForKey:@"name"];
    NSString *myName = [self primitiveName];
    [self didAccessValueForKey:@"name"];
    return myName;
}

- (void)setName:(NSString *)newName
{
    [self willChangeValueForKey:@"name"];
    [self setPrimitiveName:newName];
    [self didChangeValueForKey:@"name"];
}

@end