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

蹲街弑〆低调 提交于 2019-12-03 18:35:48

问题


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've decided to go about this (and whether it's valid is perhaps a different question) is to declare the polygon property as a transformable object, then store an NSArray containing the polygon's points (as an NSValue object).

To this end, I've written a couple of convenience class methods on my model object:

+ (NSArray *)coordsArrayFromMKPolygon:(MKPolygon *)polygon pointCount:(int)count
{
    CLLocationCoordinate2D *coords = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D) * count);
    [polygon getCoordinates:coords range:NSMakeRange(0, count)];
    NSMutableArray *coordsArray = [NSMutableArray array];
    for (int i = 0; i < count; i++) {
        NSValue *coordVal = [NSValue valueWithBytes:&coords[i] objCType:@encode(CLLocationCoordinate2D)];
        [coordsArray addObject:coordVal];
    }
    free(coords);
    return [NSArray arrayWithArray:coordsArray];
}

+ (MKPolygon *)polygonFromCoordsArray:(NSArray *)coordsArray pointCount:(int)count
{
    CLLocationCoordinate2D *coords = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D) * count);
    for (int i = 0; i < count; i++) {
        CLLocationCoordinate2D coord;
        [[coordsArray objectAtIndex:i] getValue:&coord];
        coords[i] = coord;
    }
    free(coords);
    return [MKPolygon polygonWithCoordinates:coords count:count];
}

I can just call these methods on my MKPolygon objects before saving or loading an instance of the model, but I'd like to override the dynamic getters and setters in the model itself, so that I can just say something like [turf setTurf_bounds:polygon] (where polygon is an MKPolygon instance).

What I'd really like is to be able to do something like this:

- (void)setTurf_bounds:(id)turf_bounds
{
    MKPolygon *poly = (MKPolygon *)turf_bounds;
    NSArray *coordsArray = [Turf coordsArrayFromMKPolygon:poly pointCount:[poly pointCount]];
    // Save the converted value into the @dynamic turf_bounds property
}

- (id)turf_bounds
{
    // grab the contents of the @dynamic turf_bounds property into say, NSArray *coordsArray
    return [Turf polygonFromCoordsArray:coordsArray pointCount:[coordsArray count]];
}

But I've had no joy so far. Calling [super setValue:coordsArray forKey:@"turf_bounds"] or its getter counterpart doesn't work, nor does trying to write it as self.turf_bounds (which just recursively calls my overridden setters).

Am I going about this completely the wrong way, or just missing something?


回答1:


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


来源:https://stackoverflow.com/questions/6007633/is-it-possible-to-override-getters-and-setters-for-dynamic-properties-in-an-nsm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!