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

1

10

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

Actinism answered 28/10, 2014 at 1:26 Comment(6)
Please show the code where you instantiate the objects.Iniquity
I'm seeing the same thing with Xcode 6.1 (6A1052d). I created a gist to demonstrate that it doesn't matter how I get the objects: gist.github.com/bgrace/021f55a9f160685e2433 — I would be interested to know if Printable on an NSManagedObject is working for anybody.Replete
I just ran into this issue as well trying to convert an array of NSManagedObject subclasses to a string to save in a bug report. For now I'm doing this as a workaround: var stringArray = [String]() for subclass in arrayOfSubclass { stringArray.append(subclass.name) } Then using stringArray.description to get the joined strings.Harping
how do you initialize Core Data? Are you using view controller containment?Hiltan
I have faced same issue, though it was not blank - printed as blank..Prolocutor
this limitation/bug is present as of xcode 6.2 (6C131e)... have searched all over the web but didn't find any other solution different from using NSLog.Southwest
B
8

You can use this:

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

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

Bouncy answered 26/2, 2015 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.