I have a data model with an entity called Image. This will be used to store images.
Previously I had the image as an attribute of a specific entity.
I set up the transformable stuff and it all worked.
However, now I’ve moved it into a separate entity I keep getting errors.
So… This is the Image.m file contents.
#import "Image.h"
#import "Person.h"
@implementation ImageToDataTransformer
+ (BOOL)allowsReverseTransformation {
return YES;
}
+ (Class)transformedValueClass {
return [NSData class];
}
- (id)transformedValue:(id)value {
NSData *data = UIImagePNGRepresentation(value);
return data;
}
- (id)reverseTransformedValue:(id)value {
return [[UIImage alloc] initWithData:value];
}
@end
@implementation Image
@dynamic image;
@dynamic createdDate;
@dynamic person;
@end
When I save it it doesn’t complain when I create it it doesn’t complain, I can even see it in the Person entity as an object.
(Person *) $0 = 0x1002dec0 <Person: 0x1002dec0> (entity: Person; id: 0x4f8f30 <x-coredata://2800C028-8745-45FE-854B-5783FF3FC173/Person/p6> ; data: {
avatar = "0x1007e540 <x-coredata://2800C028-8745-45FE-854B-5783FF3FC173/Image/p1>";
createdDate = nil;
email = nil;
firstname = Blah;
id = "1234567890987654321234567890";
lastName = Blah;
updatedDate = nil;
})
The avatar attribute is a relationship to an Image entity (one to one).
But if I try to access any attribute of the image I get this…
-[_NSObjectID_64_2 createdDate]: unrecognized selector sent to instance 0x1007e540
-[_NSObjectID_64_2 image]: unrecognized selector sent to instance 0x1007e540
etc etc…
Code that generates error…
NSFetchRequest *request = [NSFetchRequest fetchRequestForEntityName:@"Person"];
[request setPredicate... blah];
NSArray *results = [context executeFetchRequest...];
Person *person = [results objectAtINdex:0];
NSLog(@"%@", person.firstName); //this works fine.
NSLog(@"%@", person.avatar.createdDate); //this crashes.
Tried removing the image attribute and still getting the same problem.
Well, that was completely bizarre.
I deleted the entity and re-added it… still the same problem.
I deleted it again and added an entity called “StoredImage” instead of “Image” and it worked fine with no problems at all.
Thanks for all the help though.