问题描述
在Swift 3中,当我们使用 NSFetchRequest
时,我们必须指定 NSFetchRequestResult
。但是如何获得一系列属性值?如果我使用这样的东西
In Swift 3 when we use NSFetchRequest
, we have to specify NSFetchRequestResult
. But how to get an array of properties values? If I use something like this
let fetchRequest = NSFetchRequest<MyClass>(entityName: "MyClass")
fetchRequest.propertiesToFetch = ["myAttributeName"]
当我尝试执行获取请求时会触发异常。
错误:-executeRequest:遇到异常=数据库显示已损坏。
如果我删除了一行 propertiesToFetch
我没有得到任何错误,但我得到了一个对象数组,而不是属性。
an exception fires when I try to execute fetch request.error: -executeRequest: encountered exception = The database appears corrupt.
If I remove a line with propertiesToFetch
I don't get any errors, but I get an array of objects, not properties.
推荐答案
我忘记了为NSFetchRequest设置 resultType
。
I forgot to set resultType
for NSFetchRequest.
let fetchRequest = NSFetchRequest<MyClass>(entityName: "MyClass")
fetchRequest.propertiesToFetch = ["myAttributeName"]
fetchRequest.resultType = .dictionaryResultType
这篇关于Swift 3. NSFetchRequest propertiesToFetch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!