簡體   English   中英

更改項目名稱后Xcode 8 Core Data App崩潰

[英]Xcode 8 Core Data App Crash After Change of Project Name

我更改了Xcode 8項目的名稱,如本文中所述。 如何完全重命名Xcode項目(即包含文件夾)?

現在,在安裝了舊版本的設備上運行新版本的應用程序時,該應用程序崩潰並顯示以下錯誤消息:

[NSKeyedUnarchiver encodeObjectForKey:]:無法為密鑰(NS.objects)解碼類(OLD_PROJECTNAME.SomeObject)的對象; 該類可以在源代碼或未鏈接的庫中定義

因此,似乎舊名稱不適合重命名版本的核心數據堆棧。 如何更改此名稱以使應用程序可執行?

編輯:這是我AppDelegate.Swift內部的核心數據初始化代碼:

// MARK: - Core Data stack

lazy var persistentContainer: NSPersistentContainer = {
    /*
     The persistent container for the application. This implementation
     creates and returns a container, having loaded the store for the
     application to it. This property is optional since there are legitimate
     error conditions that could cause the creation of the store to fail.
    */

    let container = NSPersistentContainer(name: "Data")


    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            // Replace this implementation with code to handle the error appropriately.
            // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

            /*
             Typical reasons for an error here include:
             * The parent directory does not exist, cannot be created, or disallows writing.
             * The persistent store is not accessible, due to permissions or data protection when the device is locked.
             * The device is out of space.
             * The store could not be migrated to the current model version.
             Check the error message to determine what the actual problem was.
             */
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

// MARK: - Core Data Saving support

func saveContext () {
    let context = persistentContainer.viewContext
    if context.hasChanges {
        do {
            try context.save()
        } catch {
            // Replace this implementation with code to handle the error appropriately.
            // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            let nserror = error as NSError
            fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
        }
    }
}

這是一個生產應用程序,用戶在更新后不應丟失任何數據。

您在評論中提到您具有一些可變形的屬性。 這與您的錯誤消息一致,因為[NSKeyedUnarchiver decodeObjectForKey:]NSCoding一部分,並且由於Core Data使用具有可轉換屬性的NSCoding

這不是核心數據問題。 如果對這些屬性的值使用NSCoding並將結果寫入文件,則會收到相同的錯誤。 在Swift中,類的全名類似於AppName.ClassName 如果AppName不匹配,則NSCoding無法解碼該對象。 您以OLD_PROJECTNAME.SomeObject類的名稱保存了對象,但是現在您的類名是NEW_PROJECTNAME.SomeObject NSCoding處理。 您需要幫助。

要解決此問題,您需要告訴歸檔系統要使用的類。 您可以使用NSKeyedUnarchiver類方法setClass(_:forClassName:)做到這setClass(_:forClassName:) 您可能需要做一些嘗試才能正確使用語法,但是您會遇到類似

NSKeyedUnarchiver.setClass(SomeClass.self, forClassName:"OLD_PROJECT_NAME.SomeObject")

在嘗試創建使用此類的任何對象之前,必須執行此操作,以免影響創建這些對象的方式。 由於您正在使用Core Data,因此意味着在做任何以任何方式接觸Core Data的事情之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM