I'm having class that inherits NSManagedObject
that was generated using my db model:
// .h
@interface Sketch : NSManagedObject
@property (nonatomic, retain) NSDate * added;
@property (nonatomic, retain) NSString * board;
@property (nonatomic, retain) NSString * filepath;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * filename;
@end
// .m
@implementation Sketch
@dynamic added;
@dynamic board;
@dynamic filepath;
@dynamic title;
@dynamic filename;
@end
I'm using that class instances in UITableView
. Now I need to add some instances that are not stored in db (just to show them in the list):
Sketch sketch = [[Sketch alloc] init];
But when trying to set instance properties
sketch.title = @"test title";
I'm getting exception:
-[Sketch setTitle:]: unrecognized selector sent to instance 0x7ff112c13e30
Does it mean I have to create instance by adding them to Managed Context only (even if I'm not going to store them)?
[NSEntityDescription insertNewObjectForEntityForName:SKETCH_ENTITY
inManagedObjectContext:context];
No, you can create instances of NSManagedObject
subclasses and add them to the managed object context later (while I'd suggest not do do so). You have some issue with your Sketch
object, not with NSManagedObject
and NSManagedObjectContext
.
The only thing is that you should create it like this:
NSManagedObjectContext *moc = ... // your managed object context
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sketch"
inManagedObjectContext:moc];
// note nil for context
Sketch *unassociatedObject =
(Sketch *)[[NSManagedObject alloc] initWithEntity:entity
insertIntoManagedObjectContext:nil];
For more details see this answer.