Delete object in core data, failed to match the Swift Array Element type

When I’m trying to delete objects from my core data I get this error:

fatal error: NSArray element failed to match the Swift Array Element type

and I have to idea why this happens. My table view is divided into sections, maybe that has something to do with it? I have never had any problem with deleting core-data from a table view before, so this is very strange to me.

  • Inefficient parsing of Vertex and Index data, looking for more efficient methods
  • Bug in Swift array subscript indexing?
  • How to sort an Array of Tuples?
  • Declaring, Properties, Synthesizing, and Implementing int array in Objective C
  • filtering NSFetchedResultController via searchBar without loading base pauses?
  • How to return an inout (reference) in a swift function?
  • My code looks like this:

    var userList = [User]()
    var usernames = [String]()
    
            viewDidLoad(){
            let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
            let context:NSManagedObjectContext = appDel.managedObjectContext!
    
            let fetchReq = NSFetchRequest(entityName: "User")
            let en = NSEntityDescription.entityForName("User", inManagedObjectContext: context)
            let sortDescriptor = NSSortDescriptor(key: "username", ascending: true)
            fetchReq.sortDescriptors = [sortDescriptor]
            fetchReq.propertiesToFetch = ["username"]
            fetchReq.resultType = .DictionaryResultType
    
            userList = context.executeFetchRequest(fetchReq, error: nil) as [User]
    }
    
    
            func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    
                let editedCell = self.tv.cellForRowAtIndexPath(indexPath)
    
                let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
                let context:NSManagedObjectContext = appDel.managedObjectContext!
    
                if editingStyle == UITableViewCellEditingStyle.Delete {
    
                    if let tv = tableView as Optional{
    
                        let textLbl = editedCell?.textLabel?.text
                        let ind = find(usernames, textLbl!)! as Int
    
                        context.deleteObject(userList[ind] as NSManagedObject)
    
                        userList.removeAtIndex(ind)
    
                        tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
                    }
              } 
        }
    

    In my code, the usernames array is just an array with all the usernames retrieved from core data in userList.

    The errors appears last in my code where I’m trying to delete the object from the context, and from the userList; it’s the same error in both lines. I’ve tried casting my userList as Array<AnyObject> but then I also got an runtime error with zero clues to what was wrong.

    Any suggestions on how to solve this issue would be very appreciated.

    Solutions Collect From Internet About “Delete object in core data, failed to match the Swift Array Element type”

    With

    fetchReq.resultType = .DictionaryResultType
    

    the fetch request

    userList = context.executeFetchRequest(fetchReq, error: nil) as [User]
    

    returns an array of NSDictionary objects, not an array of User objects, and you are just deluding the compiler with the cast
    as [User].

    For performance reasons, the Swift runtime does not verify at this point
    if all array elements are really User objects, so this assignment
    succeeds. But as soon as you access an array element, for example with

    userList[ind]
    

    then you get the runtime exception because the element type (NSDictionary) does not match the array type (User).

    Also you cannot cast a dictionary back to the managed object,
    so this will never work:

    context.deleteObject(userList[ind] as NSManagedObject)
    

    The best solution is probably just to remove the lines

    fetchReq.propertiesToFetch = ["username"]
    fetchReq.resultType = .DictionaryResultType
    

    so that the fetch request returns an array of User objects, and
    adjust the rest of your code if necessary.

    You might have a look again at the two different solutions proposed
    in https://stackoverflow.com/a/28055573/1187415.
    The first one returns an array of managed objects, and the second one
    an array of dictionaries. What you have done here is to mix the
    solutions by setting the result type to .DictionaryResultType,
    but treating the result as an array of managed objects.

    Remark: I would recommend to use a NSFetchedResultsController
    to display the results of a Core Data fetch request in a table view.
    The FRC efficiently manages the table view data source (with optional
    grouping into sections), and updates the table view automatically
    if the result set changes.