首页 > 代码库 > swift CoreData中CoreData: warning: Unable to load class named问题

swift CoreData中CoreData: warning: Unable to load class named问题

问题

用swift创建CoreData项目时,操作CoreData Modal时,出现下面错误

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

解决办法

1. 在book.swift 中添加 @objc(Book)

2. 修改CoreData模板

/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Core Data/NSManagedObject subclass.xctemplate复制到~/Library/Developer/Xcode/Templates下,如果没有Templates文件夹,创建文件夹Templates

修改___FILEBASENAME___.swift 文件,添加@objc(___FILEBASENAMEASIDENTIFIER___),如下

import Foundation
import CoreData

@objc(___FILEBASENAMEASIDENTIFIER___)
class ___FILEBASENAMEASIDENTIFIER___: ___COREDATAMANAGEDOBJECTSUPERCLASS___ {

___COREDATAPROPERTYIMPLEMENTATIONS___
}

重启Xcode,新建NSManagedObject subClass就会自动添加@objc($className)了。

参考:

http://stackoverflow.com/questions/25076276/unabled-to-find-specific-subclass-of-nsmanagedobject



swift CoreData中CoreData: warning: Unable to load class named问题