从“NSPersistentStoreResult”投射到不相关类型“实体”始终失败 [英] Casting from 'NSPersistentStoreResult' to unrelated type 'Entity' always fails

查看:550
本文介绍了从“NSPersistentStoreResult”投射到不相关类型“实体”始终失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个小应用程序,以了解CoreData中的多对多关系。但是使用下面的代码,我得到一个错误从我的NSFetchResult转换到我的Entity类('Groepering'):

I am creating a small app to learn the many to many relationships in CoreData. However using the code below I get an error casting from my NSFetchResult to my Entity class ('Groepering'):

与我的项目相比,我在互联网上发现的样本没有什么区别,为什么演员仍然失败?

I do not see any difference from samples I found on the internet compared to my project, why does the cast still fail?

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext

let fetchRequest : NSFetchRequest = NSFetchRequest(entityName: "Entity")

    do {
        let fetchResults = try managedContext.executeRequest(fetchRequest)
        let groeperingen = fetchResults as! [Entity]
        // Here I get the Error: ^

    } catch {
        print("Error")
}


推荐答案

使用NSManagedObjectContext的 executeFetchRequest 执行NSFetchRequest。

Use executeFetchRequest method of NSManagedObjectContext when performing a NSFetchRequest.

编辑Swift 3 :对于Swift 3,请使用:

Edit Swift 3: For Swift 3, use:

let result = try managedContext.fetch(fetchRequest)

这篇关于从“NSPersistentStoreResult”投射到不相关类型“实体”始终失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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