当使用 Mirror 访问 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
}
如何在 NSManagedObject 上使用反射机制。
综合了 Core Data 属性的访问器方法
在运行时动态地。
您可以使用以下方法枚举核心数据实体的属性
这entity
的财产NSManagedObject
这是一个NSEntityDescription
并有一个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)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)