当我从核心数据中读取时,我需要获得属性的名称和值,有什么简单的方法吗?
我一直试图以Dictionary的形式检索对象模型中的第一个元素,但这给了我一个错误:
致命错误:NSArray元素与Swift Array元素不匹配型
我的代码如下:
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
有一个丰富的API用于内省。
let activeIndex = retrievedList[0]
for (key, value) in activeIndex.entity.attributesByName {
println("(key) : (activeIndex.valueForKey(key as NSString))")
}