Swift - NSFetchRequest error

984 views Asked by At

Entire code:

import UIKit
import CoreData

class InformationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate {



    @IBOutlet var recipeNameLabel: UILabel!
    var recipeName: String?

    @IBOutlet var recipeImageView: UIImageView!
    var recipeImage: UIImage?

    @IBOutlet var RecipeHowToDo: UILabel!
    var howToDo: String?

    @IBOutlet var recipeIngredientsTableView: UITableView!
    var ingredientsListArray: [String] = []

    let moc:NSManagedObjectContext? = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
    var fetchedResultsController: NSFetchedResultsController?

    override func viewDidLoad() {
        recipeNameLabel.text = recipeName
        recipeImageView.image = recipeImage


        fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest(), managedObjectContext: moc!, sectionNameKeyPath: nil, cacheName: nil)
        fetchedResultsController?.delegate = self
        fetchedResultsController?.performFetch(nil)
    }

    func fetchRequest() -> NSFetchRequest {
        var request = NSFetchRequest(entityName:"IngredientsList")
        let sortDescriptor = NSSortDescriptor(key: "ingredient", ascending: true)
        request.predicate = nil
        request.sortDescriptors = [sortDescriptor]
        request.fetchBatchSize = 20
        return request
    }


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return fetchedResultsController?.sections?[section].numberOfObjects ?? 0
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("ingredientCell", forIndexPath: indexPath) as! UITableViewCell
        if let ingredient = fetchedResultsController?.objectAtIndexPath(indexPath) as? IngredientsList {
            cell.textLabel?.text = ingredient.ingredient
        }
        return cell
    }


    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
        }
        switch editingStyle {
        case .Delete:
            moc?.deleteObject(fetchedResultsController?.objectAtIndexPath(indexPath) as! IngredientsList)
        case .Insert:
            break
        case .None:
            break
        }
    }

}

enter image description here

Error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSFetchRequest could not locate an NSEntityDescription for entity name 'IngredientsList''

Anyone, any idea?

EDIT It was like this

and this happened:

1

There are 1 answers

1
Becky Hansmeyer On

Did you create an entity in the data modeling tool in Xcode and then set its class to "IngredientsList"? In the right-hand Utilities pane, it should look something like this:

enter image description here