Printing NSManagedObject subclassed Core Data obje

2019-03-26 06:48发布

问题:

I am working on an Swift app with Core Data. I created my *.xcdatamodeld file and created a NSManagedObject Subclass (Editor -> Create NSManagedObject Subclass...).

Everything works fine except when I try to println an instantiated object of that class (lets call it Person) the console prints blank or simply Optional() if not unwrapped.

I tried adding DebugPrintable or Printable via class extension without success.

Is this a known limitation of CoreData objects? What am I missing?

Adding code for clarity:

/// Person.swift (auto-generated by Xcode)
class Person: NSManagedObject {

   @NSManaged var firstname: String
   @NSManaged var lastname: String

}

My extension:

/// Person+Helpers.swift
extension Person : Printable, DebugPrintable {
   override var description : String {
       return "test"
   }

   override var debugDescription : String {
       return "debug test"
   }
}

Console prints empty line, or in the event of an array of Person objects, it simply prints [ , , , , ]

回答1:

You can use this:

NSLog("My managed object: %@", managedObject)

For some reason, it won't output using println but NSLog works just fine.