Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to override getters and setters for @dynamic properties in an NSManagedObject subclass?
    text
    copied!<p>So, my scenario is this:</p> <p>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). </p> <p>To this end, I've written a couple of convenience class methods on my model object:</p> <pre><code>+ (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 &lt; count; i++) { NSValue *coordVal = [NSValue valueWithBytes:&amp;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 &lt; count; i++) { CLLocationCoordinate2D coord; [[coordsArray objectAtIndex:i] getValue:&amp;coord]; coords[i] = coord; } free(coords); return [MKPolygon polygonWithCoordinates:coords count:count]; } </code></pre> <p>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 <code>[turf setTurf_bounds:polygon]</code> (where polygon is an MKPolygon instance).</p> <p>What I'd really like is to be able to do something like this:</p> <pre><code>- (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]]; } </code></pre> <p>But I've had no joy so far. Calling <code>[super setValue:coordsArray forKey:@"turf_bounds"]</code> or its getter counterpart doesn't work, nor does trying to write it as self.turf_bounds (which just recursively calls my overridden setters).</p> <p>Am I going about this completely the wrong way, or just missing something?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload