无法在NSManagedObject类“ Item”上调用指定的初始化程序 [英] Failed to call designated initializer on NSManagedObject class 'Item'

查看:57
本文介绍了无法在NSManagedObject类“ Item”上调用指定的初始化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用核心数据向模型添加/删除项(NSManagedObject)。

I am using core-data to add/delete 'Items' (NSManagedObject) to the model.

但是,我收到标题错误:

However, I am receiving the error in the title:

代码:
呼叫失败NSManagedObject类'Item'
上指定的初始值设定项,如果您能告诉我我要去哪里,我将不胜感激。我认为问题在于初始化Item。

Code: Failed to call designated initializer on NSManagedObject class 'Item' I would really appreciate it if you could tell me where I am going wrong. I presume that the problem is to do with initialising Item.

RootViewController.m

RootViewController.m

- (void)AddNew {
    CDManager *manager = [[CDManager alloc] initWithManagedObjectContext:[self managedObjectContext] andDelegate:self];
    [manager addNewObject:[Item itemWithDescription:@"testing" dateSet:[NSDate date] fullfillBy:[NSDate date]]];
    [manager release];
}

CDManager.m

CDManager.m

- (id) initWithManagedObjectContext:(NSManagedObjectContext *)context andDelegate:(id<CDManagerDelegateProtocol>)delegate {
    if ((self = [super init])) {
        [self setDelegate:delegate];
        [self setContext:context];
        [self setItems:[[NSMutableArray alloc] init]];
        [self updateItems];
    }
    return self;
}

- (void)addNewObject:(Item *)item {
    NSManagedObjectContext *context = _context;
    Item *items = [NSEntityDescription
                            insertNewObjectForEntityForName:@"Item" 
                            inManagedObjectContext:_context];
    [items setDateSet:[item dateSet]];
    [items setDateToFullfill:[item dateToFullfill]];
    [items setItemDescription:[item itemDescription]];
    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Couldn't save due to : %@", [error localizedDescription]);
    }
    [_delegate manager:self didAddNewItem:items];
    [self update];
}

Item.m

static Item *shared = nil;
@implementation Item
......

+ (Item *)itemWithDescription:(NSString *)d dateSet:(NSDate *)date fullfillBy:(NSDate *)dates {
    @synchronized(shared) {
        if (!shared || shared == NULL) {
            shared = [[Item alloc] init];
        }
        [shared setItemDescription:d];
        [shared setDateSet:date];
        [shared setDateToFullfill:dates];
        return shared;
    }
}


推荐答案

Item 类定义为单例。没有提供单例NSManagedObject子类。上下文不了解如何处理。

The Item class is defined as a singleton. There is no provision for singleton NSManagedObject subclasses. The context won't understand how to handle it.

这段代码毫无意义。在这里,您初始化一个单例 Item 对象:

This code makes very little sense. Here you initialize a singleton Item object:

[manager addNewObject:[Item itemWithDescription:@"testing" dateSet:[NSDate date] fullfillBy:[NSDate date]]]

...,但是在将其传递给 addNewObject:方法之前,请不要将其附加到上下文。反过来,该方法创建另一个Item实例,然后使用假定的单例值填充它。为什么?如果您的单例代码确实有效,则每次创建Item实例时,都将获得相同的对象。创建另一个对单例的引用并为其设置自己的值有什么意义?如果单例代码不起作用,为什么首先要使用单例?

... but you don't attach it to a context before passing it to the addNewObject: method. In turn that method creates another instance of Item and then populates it with the values of the presumed singleton. Why? If your singleton code actually works, every time you create an Item instance, you will get the same object back. What is the point of creating yet another reference to the singleton and setting its own values to itself. If the singleton code doesn't work, why use a singleton in the first place?

像这样的用法就是为什么单身人士的表现如此糟糕。除非您对单例有很多经验并且绝对必要,否则不要使用单例。这段代码中没有任何内容表明您确实需要单例,而Core Data绝对不喜欢它们。

Uses like this is why singletons have such a bad rep. Don't use singletons unless you have a lot of experience with them and they are absolutely needed. There is nothing in this code that suggests that you do need a singleton and Core Data definitely does not like them.

这篇关于无法在NSManagedObject类“ Item”上调用指定的初始化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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