核心数据实体的Init方法不可用 [英] Init method for core data entity not available

查看:149
本文介绍了核心数据实体的Init方法不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在xcode 6.1上开发iOS 8.1。我有一个名为 CoreDataTest 的简单项目,其中我使用核心数据。

I am on xcode 6.1 and developing for iOS 8.1. I have simple project called CoreDataTest where I play around with core data.

模型:

import CoreData

class Licence: NSManagedObject {
    @NSManaged var name: String
}

现在我想创建一个许可证对象。这是我需要在创建对象之前:

Now I want to create a licence object. This is what I need prior to creating the object:

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

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

问题是xcode不为实体提供正确的构造函数。当我开始输入:

The problem is that xcode does not provide the correct constructor for the entity. When I start typing:

var newLicence = Licence(..

我没有自动完成,例如:

I get no auto completion like in this example:

我只是得到NSManagedObject ?

I just get the NSManagedObject() as auto completion. Any ideas whats wrong?

推荐答案

通过阅读 NSManagedObject :)

Solved it by reading the docs for NSManagedObject :)


正确配置托管对象以使用Core Data使用
很重要。如果直接实例化一个管理对象,你必须
调用指定的初始化器
(initWithEntity:insertIntoManagedObjectContext:)

It is important that a managed object is properly configured for use with Core Data. If you instantiate a managed object directly, you must call the designated initializer (initWithEntity:insertIntoManagedObjectContext:)

所以在我的类中我实现了:

So in my class I implemented:

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

这篇关于核心数据实体的Init方法不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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