无法实例化NSManagedObject的子类

无法实例化NSManagedObject的子类

本文介绍了无法实例化NSManagedObject的子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xcode 6有很多错误。但我不知道这是否是一个错误。这可能不是因为这是我刚刚学习的东西。

Xcode 6 has had a ton of bugs. But I'm not quite sure if this is a bug or not. It might not be since this is something I'm just now learning.

我的问题是,任何时候我试图实例化我的NSManagedObject的子类,我没有选项来传递构造函数的实体:NSEntityDescription NSManagedContext:insertIntoManagedContext 参数,Xcode说Extra Argumententity' in call

My issue is, any time I try to instantiate my subclass of NSManagedObject, I do not have the option to pass the entity: NSEntityDescription and NSManagedContext: insertIntoManagedContext argument to the constructor, Xcode says "Extra Argument 'entity' in call"

我从头开始创建了一个新的Xcode项目,只是为了看看是否可以在一个更小,最小的项目中重新创建问题。

I created a new Xcode project from scratch, just to see if I could re-create the problem in a smaller, minimal project.

ToDoList.Item设置为数据模型检查器中的 Item 实体类。

ToDoList.Item is set as the Item entity class in the Data Model Inspector.

下面是代码:

override func viewDidLoad() {

    super.viewDidLoad()

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context: NSManagedObjectContext = appDel.managedObjectContext!

    let ent = NSEntityDescription.entityForName("Item", inManagedObjectContext: context)!

    //compiler complains here
    var item = Item(entity: ent, insertIntoManagedObjectContext: context)!

}

这是子类:

import UIKit
import CoreData

class Item: NSManagedObject {

    @NSManaged var title: String
    @NSManaged var completed: Bool

}

全部帮助被赞赏。

推荐答案

刚刚遇到同样的问题:

Just came across the same problem: Constructor for core data entity not available

显然,我们必须实现

init(entity: NSEntityDescription, insertIntoManagedObjectContext context, NSManagedObjectContext?)

方法。所以只需添加

override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?) {
    super.init(entity: entity, insertIntoManagedObjectContext: context)
}

这篇关于无法实例化NSManagedObject的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:20