在 Swift 项目中使用 CoreData 时,使用Xcode自动生成的 Entity 类定义:
import Foundation
import CoreData
//下面这行是需要手工添加的
[color=red][b]@objc(ToDoItem)[/b][/color]
class ToDoItem: NSManagedObject {
@NSManaged var id: NSNumber
@NSManaged var name: String
@NSManaged var completed: NSNumber
}
需要手工加上:
@objc(ToDoItem)
否则以后使用时会出现一下错误:
CoreDataDemo[13807:2434800] CoreData: warning: Unable to load class named 'NewsItem' for entity 'NewsItem'. Class not found, using default NSManagedObject instead.
fatal error: NSArray element failed to match the Swift Array Element type
import Foundation
import CoreData
//下面这行是需要手工添加的
[color=red][b]@objc(ToDoItem)[/b][/color]
class ToDoItem: NSManagedObject {
@NSManaged var id: NSNumber
@NSManaged var name: String
@NSManaged var completed: NSNumber
}
需要手工加上:
@objc(ToDoItem)
否则以后使用时会出现一下错误:
CoreDataDemo[13807:2434800] CoreData: warning: Unable to load class named 'NewsItem' for entity 'NewsItem'. Class not found, using default NSManagedObject instead.
fatal error: NSArray element failed to match the Swift Array Element type