ios - 从核心数据读取时获取属性名称和值

标签 ios swift core-data dictionary

当我从我的核心数据中读取时,我需要获取属性名称和属性值,有什么简单的方法可以做到这一点吗?

我一直在尝试将我的对象模型中的第一个元素检索为字典,但这给了我一个错误提示:

fatal error: NSArray element failed to match the Swift Array Element type

我的代码是这样的:

    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext!

    let fetchReq = NSFetchRequest(entityName: "ActiveIndexes")
    fetchReq.resultType = NSFetchRequestResultType.DictionaryResultType

    receivedList = context.executeFetchRequest(fetchReq, error: nil) as! [ActiveIndexes]

    println(receivedList[0])

如有任何建议,我们将不胜感激。

最佳答案

NSManagedObject 具有丰富的内省(introspection) API。

let activeIndex = retrievedList[0]
for (key, value) in activeIndex.entity.attributesByName {
    println("\(key) : \(activeIndex.valueForKey(key as NSString))")
}

关于ios - 从核心数据读取时获取属性名称和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28864153/

相关文章:

ios - 获取图像后从其 imageView 刷新 UITableViewCell 布局

swift - 如何使用 Combine 遍历发布者的输出?

iphone - 搜索大量数据来自iPhone中的Web服务

ios - 从 uitextfield 获取值而不带可选

ios - 是否可以在一台 iPad 上运行的两个应用程序之间发送数据

ios - 在 UICollectionView 单元格中隐藏按钮

ios - CoreData : Fetch and sort results in the order they were created, 不使用时间戳

ios - 如何创建一个可以被多个 UIVIewController 共享/使用的 UIView?

ios - 为什么我的详细 View 没有反射(reflect)我的选择?

ios - 为 Transformable 属性更改 Transformer 时是否需要进行核心数据迁移?