【问题标题】:Getting both attribute name and value when reading from core data从核心数据读取时获取属性名称和值
【发布时间】:2015-03-04 20:22:50
【问题描述】:

在读取核心数据时,我需要同时获取属性名称和属性值,有什么简单的方法吗?

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

致命错误: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])

任何建议将不胜感激。

【问题讨论】:

标签: ios swift core-data dictionary


【解决方案1】:

NSManagedObjects 拥有丰富的自省 API。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多