Emptyless Emptyless - 2 years ago 134
Swift Question

Casting from 'NSPersistentStoreResult' to unrelated type 'Entity' always fails

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'):

enter image description here

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 {

Answer Source

Use executeFetchRequest method of NSManagedObjectContext when performing a NSFetchRequest.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download