小编MyN*_*ame的帖子

Swift 3 - NSFetchRequest不同的结果

任何帮助赞赏.

Xcode自动更新到8 ...我正在针对IOS 9.3

让所有的代码都被转换,但现在有一件事情已经破裂,我在类似的问题中尝试了各种建议!我取请求以前的工作现在已经断裂.

我的目标是获得一份清晰的清单.该应用程序崩溃了:

let results = try context.fetch(fetchRequest) 
Run Code Online (Sandbox Code Playgroud)

控制台中描述的错误为:

Could not cast value of type 'NSKnownKeysDictionary1' (0x10fd29328) to 'MyApp.BodyType' (0x10eebc820).
Run Code Online (Sandbox Code Playgroud)

这是功能

func getBodyTypes() {
            let context = ad.managedObjectContext
            let fetchRequest = NSFetchRequest<BodyType>(entityName: "BodyType")
            fetchRequest.propertiesToFetch = ["name"]
            fetchRequest.returnsDistinctResults = true
            fetchRequest.resultType = NSFetchRequestResultType.dictionaryResultType

            do {
                let results = try context.fetch(fetchRequest)

                for r in results {
                    bodyTypes.append(r.value(forKey: "name") as! String)
                }
            } catch let err as NSError {
                print(err.debugDescription)
            }
}
Run Code Online (Sandbox Code Playgroud)

如果下面的行是隐藏的,它不会破坏,但后来我没有得到我想要的!

fetchRequest.resultType = NSFetchRequestResultType.dictionaryResultType 
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用所有结果(4300)并将它们作为一个bandaid修复程序循环,但这并不是解决这个问题的正确方法,特别是因为它之前正在工作!

distinct nsfetchrequest ios swift

10
推荐指数
1
解决办法
4215
查看次数

标签 统计

distinct ×1

ios ×1

nsfetchrequest ×1

swift ×1