I'm trying to make a CoreData-persisted entity. This entity has a few properties, one of which is a non-standard attribute, state. The state attribute is a pointer to a C struct with a few properties in it. Here's what my data model looks like:
Entities:
MDInstance
Attributes:
- duration: Integer 16
- moves: Integer 16
- name: String
- state: Transformable. I set the Transformer to MDStateTransformer.
I generated my class and edited the state property. This is what the interface looks like:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "MDState.h" // this is where the State structure is defined
@interface MDInstance : NSManagedObject
@property (nonatomic, retain) NSNumber * duration;
@property (nonatomic, retain) NSNumber * moves;
@property (nonatomic, retain) NSString * name;
@property (nonatomic) State *state; // note that this is a pointer to a struct
@end
This is what the implementation looks like:
#import "MDInstance.h"
@implementation MDGameInstance
@dynamic duration;
@dynamic moves;
@dynamic name;
@synthesize state;
@end
I created a value transformer called MDStateTransformer. The implementation of this is (probably) not important. Suffice it to say I have allowsReverseTransformation
returning YES
, transformedValueClass
returning [NSValue class]
, and I've implemented transformedValue:
and reverseTransformedValue:
Lastly, I registered the MDStateTransformer in my AppDelegate's application:didFinishLaunchingWithOptions:
like this:
MDStateTransformer *transformer = [[MDStateTransformer alloc] init];
[NSValueTransformer setValueTransformer:transformer forName:@"MDStateTransformer"];
If I create a new instance of my MDInstance, set it's attribute -including the state attribute- and then try to save the entity, my transformer is never called.
I put a stop point in my transformer's init method and it is being instantiated. I put another in transformedValue:
and it the function is not being called.
However, if I update my MDInstance so that the state attribute is not a pointer, but is simply a State structure, and I update the transformer to work with a struct and not a pointer, the transformedValue:
is called.
Is it possible to have a custom attribute on an object which is a pointer to a c struct? If so, any ideas what I'm doing wrong?
Your transformable attribute also needs to be specified as
@dynamic
and not@synthesize
in your implementation file. By synthesizing that property you provide an getter and setter outside of core data and thus the Core Data provided accessors will not be used.