Problem creating NSManagedObject derived class
I am doing something wrong here... I know that
I'm using Xcode and I have created the following class using the data modeller:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Project : NSManagedObject {
@private
}
@property (nonatomic, retain) NSNumber * indent;
@property (nonatomic, retain) NSNumber * collapsed;
@property (nonatomic, retain) NSString * color;
@property (nonatomic, retain) NSNumber * project_id;
@property (nonatomic, retain) NSNumber * item_order;
@property (nonatomic, retain) NSNumber * cache_count;
@property (nonatomic, retain) NSNumber * user_id;
@property (nonatomic, retain) NSString * name;
@end
When I am trying to propagate this class with data from a JSON source using the following code:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"projects" ofType:@"json"];
if (filePath) {
NSString* jsonString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
DLog(@"JSON for Projects:%@", jsonString);
SBJsonParser* jsonParser = [SBJsonParser new];
id response = [jsonParser objectWithString:jsonString];
NSArray* array = (NSArray*) response;
NSEnumerator* e = [array objectEnumerator];
NSDictionary* dictionary;
while ((dictionary = (NSDictionary*)[e nextObject])) {
Project* project = [[Project alloc] init];
project.user_id = [dictionary objectForKey:@"user_id"];
project.name = [dictionary objectForKey:@"name"];
project.color = [dictionary objectForKey:@"color"];
project.collapsed = [dictionary objectForKey:@"collapsed"];
project.item_order = [dictionary objectForKey:@"item_order"];
project.cache_count = [dictionary开发者_如何转开发 objectForKey:@"cache_count"];
project.indent = [dictionary objectForKey:@"indent"];
project.project_id = [dictionary objectForKey:@"project_id"];
[elementArray addObject:project];
[project release];
}
}
However, the code stops at the project.user_id = [dictionary objectForKey:@"user_id"];
line with an exception "* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Project setUser_id:]: unrecognized selector sent to instance 0x590bcb0'"
I don't know why this is happening or how to resolve this.
I've set up a reality distortion field so I don't violate my NDA. And now I can answer your question, it has nothing to do with the product-that-must-not-be-named anyway.
There is your bug: Project* project = [[Project alloc] init];
The @dynamic setters and getters are not created for you if you create your object this way.
You can't use NSManagedObjects without a NSManagedObjectContext.
You should use something like this:
Project *project = (Project *)[NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:self.managedObjectContext];
Property names with underscores are not very sensible in the Objective C world - I guess the properties generated by Core Data have the wrong names therefore. Try using CamelCase, that is calling your properties userID
, itemOrder
, cacheCount
etc.
You may need to set up your getters and setters.
It could be as simple as adding: @synthesize user_id;
In your class file.
精彩评论