I'm trying to override the setter of an NSManagedObject
so that I can pass in an object of a different type, do a transformation and then set the property. Something like this:
- (void)setContentData:(NSData *)contentData
{
NSString *base64String;
// do some stuff to convert data to base64-encoded string
// ...
[self willChangeValueForKey:@"contentData"];
[self setPrimitiveValue:base64String forKey:@"contentData"];
[self didChangeValueForKey:@"contentData"];
}
So, in this case the contentData
field of my NSManagedObject
is an NSString *
, and I want to allow the setter to accept an NSData *
which I would then convert to an NSString *
and save it to the model. However, if I try to do this I get warnings from the compiler about trying to assign an NSData *
to an NSString *
:
myObject.contentData = someNSData;
-> Incompatible pointer types assigning to 'NSString *' from 'NSData *__strong'
Is there a better way to go about this, or perhaps I should avoid the setters altogether and create custom "setters" that allow me to pass in the NSData *
and set the NSString *
field without a compiler warning?