CoreData:警告:无法加载名为的类 [英] CoreData: warning: Unable to load class named

查看:20
本文介绍了CoreData:警告:无法加载名为的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xcode 6.1 将现有的 Objective-C 电视节目应用程序复制到新的 Swift 版本,并且在使用 CoreData 时遇到了一些问题.

I am duplicating an existing Objective-C TV Show app to a new Swift version using Xcode 6.1 and am having some issues with CoreData.

我创建了一个包含 4 个实体的模型,创建了它们的 NSManagedObject 子类(在 Swift 中),并且所有文件都设置了正确的应用程序目标(用于编译源").

I have created a model of 4 entities, created their NSManagedObject subclass (in Swift), and all files have the proper app targets set (for 'Compile Sources').

每当我尝试插入新实体时,我仍然收到此错误:

I am still getting this error whenever I try to insert a new entity:

CoreData:警告:无法为实体加载名为Shows"的类'表演'.未找到类,使用默认 NSManagedObject 代替.

CoreData: warning: Unable to load class named 'Shows' for entity 'Shows'. Class not found, using default NSManagedObject instead.

几点意见:

保存到 Core Data 时,我使用父子上下文方式来允许后台线程.我通过使用以下方法设置 ManagedObjectContext 来做到这一点:

When saving to Core Data, I use the parent-child context way to allow background threading. I do this by setting up the ManagedObjectContext using:

lazy var managedObjectContext: NSManagedObjectContext? = {
  // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
  let coordinator = self.persistentStoreCoordinator
  if coordinator == nil {
    return nil
  }
  var managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)
  managedObjectContext.persistentStoreCoordinator = coordinator
  return managedObjectContext
}()

并使用以下方法保存数据:

and by saving data using:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
  var context = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
  context.parentContext = self.managedObjectContext!
  ...rest of core data saving code here...
})

推荐答案

这个警告是我们在解决 Swift 实现细节时必须处理的怪癖之一.该警告是虚假出现的,即即使您不遵循以下概述的步骤,您的设置也可能会起作用.

This warning is one of the quirks we have to deal with while the details of the Swift implementation are being ironed out. The warning occurs spuriously, i.e. your setup might work even if you do not follow the steps outlined below.

在大多数情况下,我已经能够摆脱它通过确保在模型编辑器中正确设置类.与许多其他 SOF 帖子(包括对这个问题的答案)不同,包含模块名称(如 MyApp.Shows)的建议对我没有帮助.

I have been able to get rid of it in most cases by making sure that the class is set correctly in the model editor. Unlike in many other SOF posts (including answers to this question), the suggestion to include the module name (like MyApp.Shows) has not helped me.

请务必检查以下三项:

1.
适用于 Xcode 7 beta 3 的版本

请注意,我将您的实体名称更正为更合适的单数.

适用于 Xcode 7.1 中的 Swift 2.0 的版本
(应该适用于 Xcode 7 beta 4 及更高版本)

需要删除Module中的Current Product Module"!

You need to delete the text "Current Product Module" in Module!

2.
您还应该遵循常见的建议以包含

2.
You should also follow the frequent recommendation to include

@objc(Show)

就在你的班级之上.

注意:如果您使用的是 Xcode 7 beta 4 或更高版本,则此步骤是可选的.

Note: If you are using Xcode 7 beta 4 or later, this step is optional.

3.
还要确保将创建的托管对象强制转换到正确的类,因为默认值只是 NSManagedObject.

3.
Also make sure to cast the created managed object to the proper class, as the default would be just NSManagedObject.

var newShow = NSEntityDescription.insertNewObjectForEntityForName("Show", 
                 inManagedObjectContext: context) as Show

这篇关于CoreData:警告:无法加载名为的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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