Specifically, say I have an NSManagedObject with a "statusCode" attribute set to transformable, and a reversible value transformer subclass to covert from NSStrings to NSNumbers and vice versa. The idea is to use the value transformer so that I receive JSON and a string from a "status" key in the JSON automatically maps to an NSNumber that represents that status code in an NSManagedObject. Conversely if I were to upload the NSManagedObject to a server, at that point its status attribute would be transformed from an NSNumber to a string for the JSON.
So far so good. But, what if I also want to be able to get a simple int out of the NSManagedObjec's status property, so that I can AND it with enums in code? That is, I'd lie to cover 3 cases:
myManagedObject.status = [JSONResponse valueForKey:@"status"]
(should use transformer to do NSString -> NSNumber)[JSONforUpload setValue:myManagedObject.status forKey:@"status"]
(should use transformer to do NSNumber->NSString)if(myManagedObject.status & statusInProgress) ...
where statusInProgress is an enum.I'm thinking I could temporarily disable the value transformer, however I have no idea if the NSManagedObject has a reference to it, or if I should disable it from the NSValueTransformer class, which apparently keeps a table of registered transformers?
I know that for the 3rd case I could just do [myManagedObject.status intValue]
and then do the bitwise comparison, but I'm wondering if there's any way I can have the intValue]
be returned automagically, from the user of this object's point of view.
Any ideas?
One approach would be to add a property to the transformer so that it switches between string and enum reversed values. That would work, though I ended up doing a enum<->string transformer and not using it over a transformable attribute (instead I left the managed object's attribute as int) but rather instantiating it only for the JSON <-> object conversion. After that, throughout code I just use the int attribute as is.