0

整個代碼:斯威夫特 - NSFetchRequest錯誤

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

錯誤:終止應用程序由於未捕獲的異常 'NSInternalInconsistencyException',原因是:IngredientsList ' '' NSFetchRequest不能爲實體名稱定位NSEntityDescription'

任何人,任何想法?

編輯 It was like this

and this happened:

+0

錯誤提示CoreData找不到你的模型的IngredientsList實體 - 但是你的屏幕截圖顯示了它的存在。您的上下文/持久性商店協調員是否有可能使用其他模式? – pbasdf

+0

也許吧。我如何檢查它? –

+0

更新了整個代碼! –

回答

0

你創建在Xcode的數據建模工具的實體,然後設置其類 「IngredientsList」?在右側窗格實用工具,它應該是這個樣子:

enter image description here

+0

看起來像這樣。我更新了這個問題。不知道發生了什麼! –