Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Core data Swift核心数据发布_Core Data_Ios7_Swift_Ios8 - Fatal编程技术网

Core data Swift核心数据发布

Core data Swift核心数据发布,core-data,ios7,swift,ios8,Core Data,Ios7,Swift,Ios8,我有一个非常简单的UITableView,它使用NSFetchedResultsController加载核心数据记录。我已将Objective-C代码重新写入Swift。在iOS 7模拟器上运行Swift代码时,我似乎遇到了问题,但在iOS 8上运行正常。据我所知,Swift应该向后兼容iOS 7 我在运行iOS 7而不是8时遇到的错误是: 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“NSFetchRequest无法找到实体名称“类

我有一个非常简单的
UITableView
,它使用
NSFetchedResultsController
加载核心数据记录。我已将Objective-C代码重新写入Swift。在iOS 7模拟器上运行Swift代码时,我似乎遇到了问题,但在iOS 8上运行正常。据我所知,Swift应该向后兼容iOS 7

我在运行iOS 7而不是8时遇到的错误是:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“NSFetchRequest无法找到实体名称“类别”的NSEntityDescription”

出于某种原因,当执行
NSFetchRequest
时,它不喜欢iOS 7上的我的核心数据实体
Category
,但在iOS 8上却很好,有什么想法吗

相关代码:
看起来您可能没有正确创建托管对象上下文。使用实体名称字符串创建的获取请求在与实际托管对象上下文关联之前不是“完整”的。然后,托管对象上下文将其与包含必要实体描述的托管对象模型相关联

在这种情况下,这应该发生在“获取结果”控制器的初始化过程中。检查创建FRC时是否按预期加载了正确的托管对象上下文

<> P>不同的结果分别出现在iOS7和8,考虑首先从模拟器删除APP。此外,在再次构建之前,请尝试清理项目

class CategoriesListTableViewController : CoreDataTableViewController, AddEditCategoryTableViewControllerDelegate {
    
    let kCategoryEntityID = "Category"
    let kCategoryCellID = "Category Cell"
    let kCategoryEntityParentAttributeID = "parent"
    let kCategoryEntityNameAttributeID = "name"
    let kCategoriesCacheID = "Categories"
    let kAddCategorySegue = "Add Category"
    let kEditCategorySegue = "Edit Category"
    
    var moc = NSManagedObjectContext()
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        let app = UIApplication.sharedApplication().delegate as AppDelegate
        moc = app.cdh().context
        
        //debugcode
        let mom = moc.persistentStoreCoordinator.managedObjectModel
        let entities = mom.entitiesByName
        let entityNames = entities.description
        println("All loaded entities are: \(entityNames)")
        
        self.setupFetchedResultsController()
    }
    
    func setupFetchedResultsController() // attaches an NSFetchRequest to this UITableViewController
    {
        
        
        let request = NSFetchRequest(entityName: kCategoryEntityID)
        let sortParent = NSSortDescriptor(key: kCategoryEntityParentAttributeID, ascending: true)
        let sortName = NSSortDescriptor(key: kCategoryEntityNameAttributeID, ascending: true)
        let sortDescriptors = [sortParent, sortName]
        
        request.sortDescriptors = sortDescriptors
        
        self.fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath: kCategoryEntityParentAttributeID, cacheName: kCategoriesCacheID)
    }