0

I have an array of NSManagedObjects. of which here is a truncated 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: {...})]

When I try to assign one element to a variable as the NSManagedObject Subclass (PTKRMessage)

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

I got the following error:

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

PTKRMessage is defined in PTKRMessage.swift as:

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

it could be related to cast NSManagedObject to class -> Swift dynamic cast failed but the situation and error message is different.

Community
  • 1
  • 1
  • Did you try the various solutions to http://stackoverflow.com/questions/25076276/unable-to-find-specific-subclass-of-nsmanagedobject? – Martin R Sep 06 '15 at 15:25
  • Did you set the class in the CoreData model editor? – Kametrixom Sep 06 '15 at 15:27
  • Yes I both tried adding @objc(PTKRMessage) and adding tradersofdom.PTKRMessage as class to the entity inspector. (Xcode 6.4 already prefixes it with PRODUCT_MODULE_NAME.PTKRMessage). Error message remains the same – Peter Tennekes Sep 06 '15 at 15:35
  • possible duplicate of [CoreData: warning: Unable to load class named](http://stackoverflow.com/questions/26613971/coredata-warning-unable-to-load-class-named) – Mundi Sep 06 '15 at 18:18
  • Try this instead: http://stackoverflow.com/a/26614152/427083 – Mundi Sep 06 '15 at 18:18

1 Answers1

1

Try changing it to

@objc(PTKRMessage)
class PTKRMessage: NSManagedObject {
     @NSManaged var content: String
     ...
}
Duncan Groenewald
  • 6,956
  • 4
  • 31
  • 59