ios - 核心数据实体的初始化方法不可用

标签 ios xcode core-data

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

型号:

import CoreData

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

enter image description here

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

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

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

问题是 xcode 没有为实体提供正确的构造函数。当我开始打字时:

var newLicence = Licence(..

我没有像这个例子那样自动完成:

enter image description here

我只是将 NSManagedObject() 作为自动完成。有什么想法吗?

最佳答案

通过阅读 NSManagedObject 的文档解决了这个问题:)

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:)

所以在我的课上我实现了:

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

现在可以了。

关于ios - 核心数据实体的初始化方法不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26608056/

相关文章:

ios - 当应用程序因 SIGSEGV 崩溃时执行最后一次 TFLog

iphone - 如何在核心数据实体中手动添加数据

xcode - 如何在不签名的情况下构建开源 Xcode 项目?

android - 在 iOS 和 Android 上都可用的 SQLite 之上的中级抽象

objective-c - coreplot - 总是相同的情节

ios - 强制 UIActionSheet 使用特定方向

ios - NSFileWrapper 是否将所有内容加载到内存中?

ios - 将 iOS 静态库绑定(bind)到 Xamarin.iOS 并使用 dll

iphone - 将组件添加到路径 - Objective C

ios - 自动检测一天中的时间