错误与Swift和核心数据:致命错误:使用未实现的初始化器'init(entity:insertIntoManagedObjectContext :)' [英] Error with Swift and Core Data: fatal error: use of unimplemented initializer 'init(entity:insertIntoManagedObjectContext:)'

查看:107
本文介绍了错误与Swift和核心数据:致命错误:使用未实现的初始化器'init(entity:insertIntoManagedObjectContext :)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下继承自 NSManagedObject 的类:

import Foundation
import CoreData


class Note: NSManagedObject {



    @NSManaged var text: String
    @NSManaged var name: String


     init(name: String, text:String, context: NSManagedObjectContext){

        let entity = NSEntityDescription.entityForName("Note", inManagedObjectContext: context);
        super.init(entity: entity!, insertIntoManagedObjectContext: context)

        self.text = text
        self.name = name;

    }
}



当我创建它的实例时,一切正常,但是当我搜索这些实体,我得到一个很奇怪的错误:

When I create instances of it, everything works fine, but when I make a search for these entities, I get a very odd error:

fatal error: use of unimplemented initializer 'init(entity:insertIntoManagedObjectContext:)'

这是导致错误的代码: / p>

This is the code that causes the error:

func coreDatePlayground(){

    var note = Note(name: "A new note", text: "blah", context: self.managedObjectContext!)

    println("\(note.name)  \(note.text)")

    var noote2 = Note(name: "Another note", text: "blah blah", context: self.managedObjectContext!)


    managedObjectContext?.save(nil)

    var fetch = NSFetchRequest(entityName: "Note")
    // This line causes the crash.
    var results = self.managedObjectContext?.executeFetchRequest(fetch, error: nil)
    if let objects = results{
        println(objects.count)
    }

}



我发现更改初始化器是一个方便的方法,

I found out that changing the initialiser by making it a convenience one and calling on self instead of on super gets rid of the issue, but I have no idea why.

 convenience init(name: String, text:String, context: NSManagedObjectContext){

    let entity = NSEntityDescription.entityForName("Note", inManagedObjectContext: context);
    self.init(entity: entity!, insertIntoManagedObjectContext: context)

    self.text = text
    self.name = name;

}

上面的代码可以工作,但我不知道为什么。任何人都知道发生了什么?这是一个错误还是我的错?

The code above works, but I have no idea why. Anybody knows what's going on? Is it a bug or is my fault?

推荐答案

这是记录的行为。

Swift子类不会默认继承其父类初始化器

Swift subclasses do not inherit their superclass initializers by default

甚至没有编译,因为 Child 不会自动继承 init(id:String)。此机制可确保 Child 类中的 name 正确初始化。

For example, following code does not even compile, because Child does not inherit init(id:String) automatically. This mechanism make sure name in Child class properly initialized.

class Parent {
    var id:String
    init(id:String) {
        self.id = id
    }
}

class Child:Parent {
    var name:String
    init(id:String, name:String) {
        self.name = name
        super.init(id: id)
    }
}

var child1 = Child(id:"child1")

如果你在子类中定义了方便初始化器,那么它会自动继承所有的超类指定的初始化器记录在自动初始化器继承一节

If you define only convenience initializers in subclass, then it automatically inherits all of its superclass designated initializers as documented in "Automatic Initializer Inheritance" section

这篇关于错误与Swift和核心数据:致命错误:使用未实现的初始化器'init(entity:insertIntoManagedObjectContext :)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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