核心数据崩溃了应用程序

时间:2015-05-20 20:45:59

标签: ios sqlite swift core-data

我正在构建游戏,我正在使用核心数据来保存当前的游戏级别。 一切似乎都运行良好,但现在我的应用程序在尝试从核心数据获取当前级别时崩溃。这是我的核心数据堆栈:

lazy var applicationDocumentsDirectory: NSURL = {
    let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
    return urls[urls.count-1] as! NSURL
    }()

lazy var managedObjectModel: NSManagedObjectModel = {
    let modelURL = NSBundle.mainBundle().URLForResource("GameState", withExtension: "momd")!
    return NSManagedObjectModel(contentsOfURL: modelURL)!
    }()

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
    var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("GameName.sqlite")
    var error: NSError? = nil
    var failureReason = "There was an error creating or loading the application's saved data."
    if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
        coordinator = nil
        // Report any error we got.
        var dict = [String: AnyObject]()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
        dict[NSLocalizedFailureReasonErrorKey] = failureReason
        dict[NSUnderlyingErrorKey] = error
        error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
        NSLog("Unresolved error \(error), \(error!.userInfo)")
        abort()
    }

    return coordinator
    }()

lazy var managedObjectContext: NSManagedObjectContext? = {
    let coordinator = self.persistentStoreCoordinator
    if coordinator == nil {
        return nil
    }
    var managedObjectContext = NSManagedObjectContext()
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
    }()

// MARK: - Core Data Saving support

func saveContext () {
    if let moc = self.managedObjectContext {
        var error: NSError? = nil
        if moc.hasChanges && !moc.save(&error) {
            NSLog("Unresolved error \(error), \(error!.userInfo)")
            abort()
        }
    }
}

}

这是获取当前级别的方法:

    func fetchTheCurrentLevel() {

    let appDelegate =
    UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext!
    let fetchRequest = NSFetchRequest(entityName:"CurrentLevel")
    var error: NSError?
    var fetchedResults =
    managedContext.executeFetchRequest(fetchRequest,
        error: &error) as? [NSManagedObject]



    if let results = fetchedResults {
        if count(results) > 0 {
            let currentLevel = results[results.count - 1]
            if let num = currentLevel.valueForKey("currentLevel") as? NSNumber {
                let level = num.shortValue
                levelToShow = level
            } else {
                levelToShow = 1
            }
        } else {
            println("no objects found")
            levelToShow = 1
        }
    } else {
        println("Could not fetch \(error), \(error!.userInfo)")
    }

}

最后,这是我得到的错误:CoreData:错误:-addPersistentStoreWithType:SQLite配置:(null)URL:file:/// var / mobile / Containers / Data / Application / F60E246B-19C3-4EA5-B1E7 -100EAC9DCC92 / Documents / GameName.sqlite选项:(null)...返回错误错误Domain = NSCocoaErrorDomain Code = 134100"操作无法完成。 (可可错误134100。)"并且它说原因=用于打开商店的模型与用于创建商店的模型与userInfo字典不兼容。

任何人都知道如何解决这个问题?

0 个答案:

没有答案