gpt4 book ai didi

iphone - Xcode:+CoreDataProperties.swift 问题

转载 作者:行者123 更新时间:2023-11-30 12:18:40 27 4
gpt4 key购买 nike

我正在设计一个进展顺利的应用程序,但不久前我遇到了一个问题,我必须为 CoreData 创建一个新模型,因为我对实体进行了更改。我已经升级到第四个版本了,我的应用程序遇到了另一个问题,我清理了它。现在,这就是我得到的:

“已删除”属性设置为 NSDate

Image 1

但在我尝试再次构建它后,出现以下错误:

Error Image

我想如果我对 Entity Xcode 进行了更改,Xcode 会拾取它并相应地更改任何文件!但事实似乎并非如此!

我尝试删除 +CoreDataProperties.swift 文件和“购物 list ”swift 文件,在不同的类名下重新创建“购物 list ”swift 文件,并尝试再次构建它,但出现相同的错误。这告诉我这是 CoreData 问题,而不是 Swift 问题。显然我需要 NSDate 属性,但我不知道从这里去哪里!

构建应用程序的唯一方法是注释掉 +CoreDataProperties.swift 文件中的“已删除”属性,并且它可以正常运行。

我在测试 iPhone 6 上运行该应用程序,上次对实体进行更改时,由于错误,我丢失了在手机上手动输入的所有数据。让应用程序恢复并运行的唯一方法是从手机上删除该应用程序并重新安装。我真的不想再走这条路了,因为我手机上有近 450 条不同的记录。

如果我上传到应用商店时将“已删除”属性注释掉,是否会上传失败,上传成功后是否会无法正常工作?

我宁愿先解决问题再尝试!

最佳答案

您需要做的就是所谓的轻量级迁移。在应用程序委托(delegate)中,您需要告诉应用程序查找新版本并创建 presistence 存储

    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.




*/// THIS IS THE CODE YOU NEED TO ADD
let container = NSPersistentContainer(name: "NAMEGOESHERE")
let description = NSPersistentStoreDescription()

description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true

container.persistentStoreDescriptions = [description]
// End of new code

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

这个方法对我有用。有许多关于进行轻量级迁移的教程 here并且有一个堆栈溢出答案,这是我在遇到此问题时实际提到的一个答案 here 。通过进行迁移,您的应用程序应该能够创建一个新的持久性存储,并在该新存储中将删除的属性从 Bool 更改为 NSdate,如生成的托管对象文件中所示。希望这有帮助

关于iphone - Xcode:+CoreDataProperties.swift 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45087379/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com