Swift 3 - NSFetchRequest不同的结果 [英] Swift 3 - NSFetchRequest Distinct Results

查看:498
本文介绍了Swift 3 - NSFetchRequest不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何帮助表示赞赏。

Xcode自动更新为8 ...我正在定位IOS 9.3

Xcode auto updated to 8... I am Targeting IOS 9.3

将所有代码转换为但现在有一件事情已经破裂,我在类似的问题上尝试了各种建议!我之前正在工作的 的抓取请求正在中断。

Have all of the code converted across but one thing is now breaking, I have tried various suggestions in similar questions! My fetch request that was previously working is now breaking.

我的目标是获得一个不同的列表。该应用程序在该行崩溃:

My goal is to get a distinct list. The app crashes on the line:

let results = try context.fetch(fetchRequest) 

控制台中描述的错误为:

With the error described in the console as:

Could not cast value of type 'NSKnownKeysDictionary1' (0x10fd29328) to 'MyApp.BodyType' (0x10eebc820).

这是函数

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

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

If the line below is hidden it doesn't break, but then i don't get what i want!

fetchRequest.resultType = NSFetchRequestResultType.dictionaryResultType 

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

I understand i could use all of the results (4300) and loop through them as a bandaid fix, but this doesn't feel like the correct way to fix this, especially as it was working before!

推荐答案

诀窍是使通用更通用 - 而不是< BodyType> <在您的获取请求中使用< NSFetchRequestResult>

The trick is to make the generic more general - instead of <BodyType> in your fetch request, use <NSFetchRequestResult>:

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "BodyType")

结果此获取请求是 [Any] ,因此您需要在使用之前强制转换为相应的字典类型。例如:

The result of this fetch request is [Any], so you will need to cast to the appropriate dictionary type before using. Eg:

func getBodyTypes() {
    let context = ad.managedObjectContext
    // 1) use the more general type, NSFetchRequestResult, here:
    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "BodyType")
    fetchRequest.propertiesToFetch = ["name"]
    fetchRequest.returnsDistinctResults = true
    fetchRequest.resultType = NSFetchRequestResultType.dictionaryResultType

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

        // 2) cast the results to the expected dictionary type:
        let resultsDict = results as! [[String: String]]

        for r in resultsDict {
            bodyTypes.append(r["name"])
        }

    } catch let err as NSError {
        print(err.debugDescription)
    }
}

注意: NSFetchRequestResult 是一种协议,由四种类型采用:

- NSDictionary

- NSManagedObject

- NSManagedObjectID ,和< br>
- NSNumber

Note: NSFetchRequestResult is a protocol, adopted by four types:
- NSDictionary,
- NSManagedObject,
- NSManagedObjectID, and
- NSNumber

通常情况下,我们使用 NSManagedObject ,例如 BodyType 。但是,在这种情况下,由于语句,您将获得字典类型:

Typically, we're using it with an NSManagedObject, such as your BodyType. In this case, though, you're getting the dictionary type because of the statement:

fetchRequest.resultType = NSFetchRequestResultType.dictionaryResultType

这篇关于Swift 3 - NSFetchRequest不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆