创建NSManagedObject派生类时出现问题 [英] Problem creating NSManagedObject derived class

查看:82
本文介绍了创建NSManagedObject派生类时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里做错了...我知道

I am doing something wrong here... I know that

我使用Xcode,我使用数据建模器创建了下面的类:

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

当我试图传播数据一个JSON源使用以下代码:

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

但是,代码停在 project.user_id = [dictionary objectForKey:@user_id]; 出现异常 * 由于未捕获异常而终止应用程序'NSInvalidArgumentException' [project setUser_id:]:无法识别的选择器发送到实例0x590bcb0'

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.

推荐答案

我设置了一个真实的失真字段,所以我没有违反我的NDA。现在我可以回答你的问题了,它与产品无关。

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.

有你的错误: Project * project = [[Project alloc] init];

如果您以这种方式创建对象,则不会为您创建@dynamic设置器和getter。

不能使用NSManagedObjects而不使用NSManagedObjectContext。

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.

您应该使用这样的:

Project *project = (Project *)[NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:self.managedObjectContext];

这篇关于创建NSManagedObject派生类时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆