Swift将NSManagedObject转换为子行类失败

时间:2015-09-06 15:17:56

标签: swift core-data nsmanagedobject

我有一个NSManagedObjects数组。 其中这是一个截断的println:

[<NSManagedObject: 0x7f9cbc18cf30> (entity: PTKRMessage; id: 0xd00000000004001a <x-coredata://7EEF4956-FA8F-4DE9-B94C-CF612B40AA77/PTKRMessage/p1> ; data: {...}), <NSManagedObject: 0x7f9cbc18d790> (entity: PTKRMessage; id: 0xd00000000008001a <x-coredata://7EEF4956-FA8F-4DE9-B94C-CF612B40AA77/PTKRMessage/p2> ; data: {...})]

当我尝试将一个元素作为NSManagedObject子类(PTKRMessage)分配给变量时

let message = self.tableData[indexPath.row] as! PTKRMessage

我收到以下错误:

Could not cast value of type 'NSManagedObject_PTKRMessage_' (0x7f9cbc320d10) to 'tradersofdom.PTKRMessage' (0x102dda910).

PTKRMessage在PTKRMessage.swift中定义为:

class PTKRMessage: NSManagedObject {
     @NSManaged var content: String
     ...
}

它可能与cast NSManagedObject to class -> Swift dynamic cast failed有关,但情况和错误信息不同。

1 个答案:

答案 0 :(得分:1)

尝试将其更改为

@objc(PTKRMessage)
class PTKRMessage: NSManagedObject {
     @NSManaged var content: String
     ...
}