Problem creating NSManagedObject derived class

空扰寡人 提交于 2020-01-05 05:57:10

问题


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.


回答1:


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];



回答2:


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.




回答3:


You may need to set up your getters and setters.

It could be as simple as adding: @synthesize user_id;

In your class file.



来源:https://stackoverflow.com/questions/5231033/problem-creating-nsmanagedobject-derived-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!