views:

31

answers:

2

Hi, the problem I am trying to solve in an application that is using Core Data is to be able to hold a calculated value in a NSManagedObject custom ivar. The calculated value that I want to store is in fact an image. I do not want to persist these images; I build them and destroy them throughout the lifetime of the application. I tried along the lines of:

@interface RTStaffImage : NSManagedObject {

UIImage *image;

}

// Custom properties
@property (nonatomic, retain) UIImage *image;
// Managed object properties
@property (nonatomic, retain) NSNumber *imageID;
@property (nonatomic, retain) NSString *imageName;

and custom accessors methods:

- (void)setImage (UIImage*)im;
- (UIImage *)image;

and in the implementation:

@implementation RTStaffImage

@synthesize image;
@dynamic imageID;
@dynamic imageName;

This fails at runtime with unrecognised selector problems:

-[NSManagedObject setImage:]: unrecognized selector sent to instance

The above approach is what Apple (or, at least as far as I see having read the docs) outlines for transient properties so it should work :-(

Any ideas, comments?

A: 

Yeah, you have these backwards:

@synthesize image;
@dynamic imageID;
@dynamic imageName;

You're providing an implementation for setImage and image, so image should be @dynamic, and the others you need synthesized methods for, so use @synthesize for imageID and imageName.

Good point, they should all be @dynamic since you're with CoreData.

2nd attempt: you have set RTStaffImage as the Class name in the Entity, right?

3rd attempt: is RTStaffImage.m actually part of the Target being built?

Graham Perks
No. imageID and imageName must be @dynamic since accessors for these properties are created on the fly by Core Data. I see what you are saying about image - I'll try @dynamic for that...
Andrew Coad
>> 2nd attempt: you have set RTStaffImage as the Class name in the Entity, right?Yes, I have. BTW - I tried changing @synthesize image to @dynamic image to no avail
Andrew Coad
A: 
- (void)setImage (UIImage*)im;

you are missing a colon between setImage and (UIImage*). This is the correct version:

- (void)setImage:(UIImage*)im;

And where are the implementations of those two methods?


-[NSManagedObject setImage:]: unrecognized selector sent to instance

just curious, I read NSManagedObject there, are you sure you create an instance of RTStaffImage there?

fluchtpunkt
>> you are missing a colon between setImage and (UIImage*). That's just my typo - I didn't cut and paste. I have it correct (or it wouldn't compile)
Andrew Coad
>> just curious, I read NSManagedObject there, are you sure you create an instance of RTStaffImage there? Yes. That is how Core Data works. If you subclass NSManagedObject, in my case RTStaffImage is a subclass, the debug output always says 'NSManagedObject' not the actual subclass type. Don't know why.....maybe because it didn't find the setImage method on RTStaffImage it bumped it up the responder chain???
Andrew Coad