本文介绍了打印NSManagedObject子类Core Data对象到控制台在Swift中返回空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用核心数据的Swift应用程序。我创建了 *。xcdatamodeld 文件并创建了一个 NSManagedObject子类 Editor - > Create NSManagedObject子类... )。



一切正常,除非我尝试 println 该类的实例化对象(让它调用 Person )控制台输出空白或简单 Optional()已解开。



我尝试添加 DebugPrintable 可打印类扩展未成功。



这是CoreData对象的已知限制吗?



为了清楚起见,添加代码:

  // Person.swift(由Xcode自动生成)
类Person:NSManagedObject {

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

}

我的扩展程序:

  /// Person + Helpers.swift 
扩展Person:Printable,DebugPrintable {
override var description:String {
returntest
}

override var debugDescription:String {
returndebug test
}
}

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

解决方案

b
$ b

  NSLog(我的托管对象:%@,managedObject)
println 但 NSLog 工作很好。


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

解决方案

You can use this:

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

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

这篇关于打印NSManagedObject子类Core Data对象到控制台在Swift中返回空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 22:49