1
votes

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
The error suggests that CoreData can't find an IngredientsList entity in your model - but your screenshot shows it exists. Is it possible that your context/persistent store coordinator is using a different model? - pbasdf
Maybe. How do I check it? - Nicholas Piccoli
Updated the entire code! - Nicholas Piccoli
Your managedObjectContexct is built in the App Delegate. So check there. There should be a managedObjectModel function. In that it will give the name of the model (normally the same name as your app, with extension ".momd"). Whatever name it is, it should be the same as your .xcdatamodel file in Xcode. - pbasdf
I did what you've told me, and another crash happened. I updated the question! I don't know what to do, it seems right to me. - Nicholas Piccoli

1 Answers

1
votes

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