Is it possible to override getters and setters for @dynamic properties in an NSManagedObject subclass?

后端 未结 1 898
隐瞒了意图╮
隐瞒了意图╮ 2020-12-24 05:57

So, my scenario is this:

I have an NSManagedObject subclass in my iOS application, and as a property I want to store the contents of an MKPolygon object. The way I\'

相关标签:
1条回答
  • 2020-12-24 05:57

    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
    
    0 讨论(0)
提交回复
热议问题