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

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

问题描述

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



我创建了一个模型的4个实体,创建了它们的NSManagedObject子类(在Swift中),并且所有文件都设置了适当的应用程序目标(对于Compile Sources)。



每当我尝试插入一个新实体时出错:


CoreData:警告:无法为实体
加载名为 '显示'。找不到类,使用默认NSManagedObject。


几个注释: b

当保存到Core Data时,我使用父子上下文方式允许后台线程。我这样做通过设置ManagedObjectContext使用:

  lazy var managedObjectContext:NSManagedObjectContext? = {
//返回应用程序的受管对象上下文(已经绑定到应用程序的持久存储协调器。)此属性是可选的,因为存在可能导致上下文创建的合法错误条件失败。
let coordinator = self.persistentStoreCoordinator
if coordinator == nil {
return nil
}
var managedObjectContext = NSManagedObjectContext(concurrencyType:NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
}()

资料使用:

  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),{() - > Void in 
var context = NSManagedObjectContext(concurrencyType:NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
context.parentContext = self.managedObjectContext!
...这里的核心数据保存代码...
})


解决方案

这个警告是我们必须处理的一个怪癖, Swift实现正在被解决。该警告发生的错误,即您的设置可能工作,即使你不按照下面列出的步骤。



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



请务必检查以下三个项目:



1。

适用于Xcode 7 beta 3的版本





请注意,我已将您的实体名称更正为更适当的单数。



在Xcode 7.1中适用于Swift 2.0的版本

(适用于Xcode 7 beta 4及更高版本)
/ p>

您需要删除Module中的当前产品模块文本。





2。

您还应遵循频繁的建议,包括

  @objc(Show)



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



3。

另外,请务必投放类,因为默认将只是 NSManagedObject

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


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.

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: warning: Unable to load class named 'Shows' for entity 'Shows'. Class not found, using default NSManagedObject instead.

A few comments:

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...
})

解决方案

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.

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.

Make sure you check these three items:

1.
Version that works up to Xcode 7 beta 3

Notice that I corrected your entity name to the more appropriate singular.

Version that works for Swift 2.0 in Xcode 7.1
(Should work for Xcode 7 beta 4 and above)

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

2.
You should also follow the frequent recommendation to include

@objc(Show)

just above your class.

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

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:warning:无法加载名为的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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