是否可以覆盖NSManagedObject子类中@dynamic属性的getter和setter? [英] Is it possible to override getters and setters for @dynamic properties in an NSManagedObject subclass?

查看:146
本文介绍了是否可以覆盖NSManagedObject子类中@dynamic属性的getter和setter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的方案是这样:



我有一个NSManagedObject子类在我的iOS应用程序,作为一个属性,我想存储一个MKPolygon对象的内容。我决定去做这个(和它是否有效也许是一个不同的问题)的方式是声明多边形属性作为一个可变形对象,然后存储一个NSArray包含多边形的点(作为一个NSValue对象)。



为此,我在我的模型对象上写了几个方便的类方法:

  +(NSArray *)coordsArrayFromMKPolygon:(MKPolygon *)polygon pointCount:(int)count 
{
CLLocationCoordinate2D * coords =(CLLocationCoordinate2D *)malloc计数);
[polygon getCoordinates:coords range:NSMakeRange(0,count)];
NSMutableArray * coordsArray = [NSMutableArray array];
for(int i = 0; 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 CLLocationCoordinate2D coord;
[[coordsArray objectAtIndex:i] getValue:& coord];
coords [i] = coord;
}
free(coords);
return [MKPolygon polygonWithCoordinates:coords count:count];
}



在保存或加载实例之前,我可以在我的MKPolygon对象上调用这些方法,但我想覆盖模型本身中的动态getter和setter,所以我可以只说一些 [turf setTurf_bounds:polygon] (其中多边形是一个MKPolygon实例)。



我真正想要的是能够做这样的事情:

   - (void)setTurf_bounds:(id)turf_bounds 
{
MKPolygon * poly =(MKPolygon *)turf_bounds;
NSArray * coordsArray = [Turf coordsArrayFromMKPolygon:poly pointCount:[poly pointCount]];
//将转换后的值保存到@dynamic turf_bounds属性
}

- (id)turf_bounds
{
//获取@dynamic turf_bounds属性说,NSArray * coordsArray
return [Turf polygonFromCoordsArray:coordsArray pointCount:[coordsArray count]];
}

但我到目前为止没有快乐。调用 [super setValue:coordsArray forKey:@turf_bounds] 或其getter对应函数不工作,也不尝试将其写为self.turf_bounds



>解决方案

不要在NSManagedObject子类中调用[super valueForKey:..]! (除非你自己在超类中实现它们)
而不是使用原始访问器方法。



ADC

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

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


@implementation部门

@dynamic名称;

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

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

@end


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?

解决方案

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

这篇关于是否可以覆盖NSManagedObject子类中@dynamic属性的getter和setter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆