将NSManagedObject子类化的Core Data对象打印到控制台,在Swift中返回空行

时间:2022-10-25 07:43:19

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

我正在使用Core Data的Swift应用程序。我创建了我的* .xcdatamodeld文件并创建了一个NSManagedObject子类(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.

一切正常,除非我尝试打印该类的实例化对象(让我们称之为Person),控制台打印空白或简单的Optional()如果没有打开。

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

我尝试通过类扩展添加DebugPrintable或Printable但没有成功。

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

这是CoreData对象的已知限制吗?我错过了什么?

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

控制台打印空行,或者在Person对象数组的情况下,它只打印[,,,,]

1 个解决方案

#1


8  

You can use this:

你可以用这个:

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

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

出于某种原因,它不会使用println输出,但NSLog工作正常。

#1


8  

You can use this:

你可以用这个:

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

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

出于某种原因,它不会使用println输出,但NSLog工作正常。