使用镜像访问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上使用反射机制.
解决方法
核心数据属性的访问器方法是合成的
在运行时动态.
在运行时动态.
您可以使用枚举核心数据实体的属性
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) }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。