0
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
}
}
}
錯誤:終止應用程序由於未捕獲的異常 'NSInternalInconsistencyException',原因是:IngredientsList ' '' NSFetchRequest不能爲實體名稱定位NSEntityDescription'
任何人,任何想法?
編輯
錯誤提示CoreData找不到你的模型的IngredientsList實體 - 但是你的屏幕截圖顯示了它的存在。您的上下文/持久性商店協調員是否有可能使用其他模式? – pbasdf
也許吧。我如何檢查它? –
更新了整個代碼! –