Swift: Reflecting properties of subclass of NSManagedObject

∥☆過路亽.° 提交于 2019-12-04 08:59:11

The accessor methods for Core Data properties are synthesized dynamically at runtime.

You can enumerate the attributes of a Core Data entity using the entity property of NSManagedObject which is a NSEntityDescription and has a attributesByName property.

A simple example:

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)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!