Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing NSManagedObject subclassed Core Data object to console returns empty line in Swift

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 [ , , , , ]

like image 669
opfeffer Avatar asked Oct 28 '14 01:10

opfeffer


1 Answers

You can use this:

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

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

like image 129
Simon Germain Avatar answered Oct 02 '22 15:10

Simon Germain