使用镜像访问NSManagedObject的子类的内部结构时,将忽略所有托管变量.
public class Foo: NSManagedObject { @NSManaged var bar: String? } var f: Foo = ... // ... creating a Foo in a valid context ... let mirror = Mirror(reflecting: f) for c in mirror.children { // children count == 0 print("\(c.label!):\(c.value)") // never executed }
如何在NSManagedObjects上使用反射机制.
Core Data属性的访问器方法在运行时动态合成.
您可以使用entity
属性NSManagedObject
为a NSEntityDescription
且具有属性来枚举Core Data实体的attributesByName
属性.
一个简单的例子:
for (name, attr) in newManagedObject.entity.attributesByName { let attrType = attr.attributeType // NSAttributeType enumeration for the property type let attrClass = attr.attributeValueClassName ?? "unknown" print(name, "=", newManagedObject.valueForKey(name), "type =", attrClass) }