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

时间:2015-03-04 20:22:50

标签: ios swift core-data dictionary

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

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

  

致命错误:NSArray元素无法与Swift数组元素匹配   型

我的代码如下所示:

    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])

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:1)

NSManagedObject拥有丰富的内省API。

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