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

五迷三道 提交于 2019-12-21 04:05:01

问题


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.



来源:https://stackoverflow.com/questions/26599493/printing-nsmanagedobject-subclassed-core-data-object-to-console-returns-empty-li

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!