Emptyless Emptyless - 7 months ago 22
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 {
print("Error")
}

Answer

Use executeFetchRequest method of NSManagedObjectContext when performing a NSFetchRequest.

Comments