Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.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
Ios 更改项目名称后Xcode 8核心数据应用程序崩溃_Ios_Objective C_Swift_Xcode_Core Data - Fatal编程技术网

Ios 更改项目名称后Xcode 8核心数据应用程序崩溃

Ios 更改项目名称后Xcode 8核心数据应用程序崩溃,ios,objective-c,swift,xcode,core-data,Ios,Objective C,Swift,Xcode,Core Data,我更改了我的Xcode 8项目的名称,如本文所述 现在,在安装旧版本的设备上运行新版本的应用程序时,应用程序崩溃,并显示以下错误消息: [NSKeyedUnachiver decodeObjectForKey:]:无法解码类的对象 (旧项目名称.SomeObject)用于键(NS.objects);这个班可能是 在源代码或未链接的库中定义 因此,旧名称似乎不适合重命名版本的核心数据堆栈。 如何更改此名称以使应用程序可执行 编辑: 这是myAppDelegate.Swift中的核心数据初始化代码:

我更改了我的Xcode 8项目的名称,如本文所述

现在,在安装旧版本的设备上运行新版本的应用程序时,应用程序崩溃,并显示以下错误消息:

[NSKeyedUnachiver decodeObjectForKey:]:无法解码类的对象 (旧项目名称.SomeObject)用于键(NS.objects);这个班可能是 在源代码或未链接的库中定义

因此,旧名称似乎不适合重命名版本的核心数据堆栈。 如何更改此名称以使应用程序可执行

编辑: 这是my
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
的一部分,而且核心数据使用具有可转换属性的
NSCoding

这不是核心数据问题。如果对这些属性的值使用
NSCoding
,并将结果写入文件,则会出现相同的错误。在Swift中,类的全名类似于
AppName.ClassName
。如果
AppName
不匹配,
NSCoding
无法解码对象。您保存的对象的名称类似于
OLD\u PROJECTNAME.SomeObject
,但现在您的类名类似于
NEW\u PROJECTNAME.SomeObject
NSCoding
无法单独处理此问题。你需要帮助它

要解决此问题,您需要告诉归档系统要使用哪个类。您可以使用
nskeyedunachiver
class方法
setClass(u:forClassName:)
。您可能需要进行一点实验,以获得正确的语法,但您将得到如下结果

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

在尝试创建使用该类的任何对象之前,必须执行此操作,以使其影响这些对象的创建方式。因为你使用的是核心数据,这意味着在做任何以任何方式涉及核心数据的事情之前。

你在评论中提到,你有一些可转换的属性。这与您的错误消息一致,因为
[NSKeyedUnarchiver decodeObjectForKey:
NSCoding
的一部分,而且核心数据使用具有可转换属性的
NSCoding

这不是核心数据问题。如果对这些属性的值使用
NSCoding
,并将结果写入文件,则会出现相同的错误。在Swift中,类的全名类似于
AppName.ClassName
。如果
AppName
不匹配,
NSCoding
无法解码对象。您保存的对象的名称类似于
OLD\u PROJECTNAME.SomeObject
,但现在您的类名类似于
NEW\u PROJECTNAME.SomeObject
NSCoding
无法单独处理此问题。你需要帮助它

要解决此问题,您需要告诉归档系统要使用哪个类。您可以使用
nskeyedunachiver
class方法
setClass(u:forClassName:)
。您可能需要进行一点实验,以获得正确的语法,但您将得到如下结果

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

在尝试创建使用该类的任何对象之前,必须执行此操作,以使其影响这些对象的创建方式。因为您使用的是核心数据,这意味着在执行任何涉及核心数据的操作之前。

您试图通过项目重命名实现什么?您是否只是尝试重命名应用程序,使其在应用程序图标下显示不同的名称?这是一个生产应用程序,它是一个有效的用例,还是在开发阶段对此感到疑惑?这是一个生产应用程序,我想更改项目的全名。我可以手动设置核心数据模型的名称(以适应旧的名称)或者,如何将旧数据模型导入到新数据模型中而不发生应用程序崩溃?您是否可以发布CoreData初始化代码的片段(如果是为您创建的,则可能在AppDelegate中)?您可以在此初始化代码中更改核心数据模型的名称,但可能会丢失现有用户的现有设备上数据。这可以接受吗?我编辑了这个问题。否用户不应使用任何数据。您试图通过项目重命名实现什么?您是否只是尝试重命名应用程序,使其在应用程序图标下显示不同的名称?这是一个生产应用程序,它是一个有效的用例,还是在开发阶段对此感到疑惑?这是一个生产应用程序,我想更改项目的全名。我可以手动设置核心数据模型的名称(以适应旧的名称)或者,如何将旧数据模型导入到新数据模型中而不发生应用程序崩溃?您是否可以发布CoreData初始化代码的片段(如果是为您创建的,则可能在AppDelegate中)?您可以在此初始化代码中更改核心数据模型的名称,但可能会丢失现有用户的现有设备上数据。这可以接受吗?我编辑了这个问题。不,用户不应使用任何数据。谢谢您的详细回答!现在一切都很好。对不起,先生,我需要将带有lasttargetName.class的NSKeyedUnachiver方法放在哪里,放在te对象类中还是放在取消归档之前?谢谢您的详细回答!现在一切都很好。对不起,先生,我需要将带有lasttargetName.class的NSKeyedUnarchiver方法放在哪里,放在te对象类中,还是放在取消归档之前?