Use Mirror(reflecting: ) on Core Data NSManagedObject

26 Views Asked by At

I use the following code to add an object to Core Data. Why is it when I use Mirror(reflecting: ) on the the object, nothing prints out?

Saving works fine.

var dict = [String: Any]()
dict.updateValue("abc", forKey: "uid")

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}
let context: NSManagedObjectContext = appDelegate.persistentContainer.viewContext
let entity: NSEntityDescription = NSEntityDescription.entity(forEntityName: "MyModel", in: context)!

let object: NSManagedObject = NSManagedObject(entity: entity, insertInto: context)

for (k,v) in dict {
    object.setValue(v, forKey: k)
}

let mirror = Mirror(reflecting: object)
let properties = mirror.children
for property in properties {
    print("key: \(property.label as Any) = value: \(property.value)")
}

do {
    try context.save()
} catch let error as NSError {
    print("could not save . \(error), \(error.userInfo)")
}
0

There are 0 best solutions below