【问题标题】:Using CoreData to save and retrieve data from TextFields使用 Core Data 从文本字段中保存和检索数据
【发布时间】:2016-06-17 16:23:34
【问题描述】:

节目信息

嗨!我在 Stackoverflow 上发布的第一个问题,这里是: 我目前正在编写一个用于二手零件检查的程序,并且需要能够使用 Core Data(或其他东西,如果推荐的话)保存客户信息。我只需要保存它然后检索它来制作 PDF。

问题

-我在点击保存按钮时收到错误消息: -我不知道如何检索数据并将其转换为 PDF(但这完全是一个单独的问题)



错误

*2016-06-17 11:15:54.336 NewParts[6803:2846480] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Containers/Data/Application/1AAF2587-19C6-424F-BA3A-F37BBEE4AC71/Documents/SingleViewCoreData.sqlite options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "The managed object model version used to open the persistent store is incompatible with the one that was used to create the persistent store." UserInfo={metadata={
    NSPersistenceFrameworkVersion = 641;
    NSStoreModelVersionHashes =     {
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "F4F8EB7F-B2EB-4D7F-A2A3-45FA91F8582E";
    "_NSAutoVacuumLevel" = 2;
},*

保存按钮操作

@IBAction func saveButton(sender: AnyObject)
{
    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
    var context:NSManagedObjectContext = appDel.managedObjectContext

    var entity1 = NSEntityDescription.insertNewObjectForEntityForName("UsedInfo", inManagedObjectContext:context) as NSManagedObject
    entity1.setValue("Test", forKey: "product")

    do {
        try context.save()
    }
    catch {
        print("error")
    }


}

【问题讨论】:

  • 值得注意的是,我完全是 iOS/Swift 菜鸟。

标签: ios xcode swift sqlite core-data


【解决方案1】:

您发布的错误实际上有解释

Code=134100 用于打开的托管对象模型版本 持久存储与用于创建的存储不兼容 持久存储

如果您的应用未在 AppStore 上发布,您可以简单地从设备(或模拟器)中删除应用,清理项目并执行 Build&Run。

每当您对 Core Data 定义进行更改时,您都应该删除应用并重新构建。

如果你的应用在 AppStore,你应该进行 Core Data 迁移,你可以在网上找到很多资料。

Apple Core Data Model Versioning and Data Migration,解释了如何执行自动轻量级迁移:

您使用选项请求自动轻量级迁移 你传入的字典 addPersistentStoreWithType:configuration:URL:options:error:, by 设置值对应于两个 NSMigratePersistentStoresAutomaticallyOption 和 NSInferMappingModelAutomaticallyOption 键为 YES:

这意味着您可以将以下选项传递给您的持久协调员:

let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]

coordinator.addPersistentStoreWithType(NSSQLiteStoreType,configuration: nil,URL : storeURL, options as! [NSObject : AnyObject])

【讨论】:

  • 谢谢!卸载应用程序并清理构建消除了错误。我只有最后一个问题,如果你能回答它,这样我就可以清楚一点。当我打印为文本字段“产品”输入的值时,我得到了测试值 Optional(Test),这在技术上是正确的,因为我将值设置为“测试”。知道 Optional() 部分是什么吗?我只是想确保它正确添加了所有内容。
  • 欢迎您,关于 Optional - 如果您查看 UITextfield 的属性 text : public var text: String? // 默认为零。你可以看到它是可选的字符串。所以当您看到 Optional(Test) 时,它只是意味着该值是 String 类型吗? (可选字符串)并包含值“Test”。
  • Swift 引入了可选类型,用于处理值的缺失。 Optionals 说“有一个值,它等于 x”或“根本没有一个值”你可以阅读更多:developer.apple.com/library/ios/documentation/Swift/Conceptual/…
  • 谢谢!你帮了大忙!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多