斯威夫特 - 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
}
}
}
错误:终止应用程序由于未捕获的异常 'NSInternalInconsistencyException',原因是:IngredientsList ' '' NSFetchRequest不能为实体名称定位NSEntityDescription'
任何人,任何想法?
编辑
答
你创建在Xcode的数据建模工具的实体,然后设置其类 “IngredientsList”?在右侧窗格实用工具,它应该是这个样子:
+0
看起来像这样。我更新了这个问题。不知道发生了什么! –
错误提示CoreData找不到你的模型的IngredientsList实体 - 但是你的屏幕截图显示了它的存在。您的上下文/持久性商店协调员是否有可能使用其他模式? – pbasdf
也许吧。我如何检查它? –
更新了整个代码! –