Swift CoreData 注意事项(NSArray element failed to match the Swift Array Element typ)

在 Swift 项目中使用 CoreData 时,使用Xcode自动生成的 Entity 类定义:

import Foundation
import CoreData

//下面这行是需要手工添加的
@objc(ToDoItem)
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

猜你喜欢

转载自joeyjoker.iteye.com/blog/2124916