【问题标题】:Core data id auto increment核心数据 id 自动递增
【发布时间】:2016-04-11 13:35:33
【问题描述】:

我找到了一个名为 objectID 的主题,用于获取表格每一行的自动增量 ID,我想获取并显示它,但我不明白该怎么做,我的代码:

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context: NSManagedObjectContext = appDel.managedObjectContext

    let newDemande = NSEntityDescription.insertNewObjectForEntityForName("Demandes", inManagedObjectContext: context)
    newDemande.setValue(soapRequest.xmlString, forKey: "xml")
    newDemande.setValue(1, forKey: "statutenvoie")

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

    do {
        let request = NSFetchRequest(entityName: "Demandes")
        let results = try context.executeFetchRequest(request)

        if results.count > 0 {
            for item in results as! [NSManagedObject]{
                let xml = item.valueForKey("xml")
                let statutenvoie = item.valueForKey("statutenvoie")

                print(xml!, statutenvoie!)
            }
        }
    } catch {
        print("erreur data")
    }

错误:

print(item.objectID)

2016-01-07 10:53:22.679 ...[956:28002] CoreData:错误: -addPersistentStoreWithType:SQLite 配置:(null) URL:file:///Users/informatiqueresponis/Library/Developer/CoreSimulator/Devices/FF92B583-0288-41FE-AD09-718E7E66D457/data/Containers/Data/Application/B851E6CE-C0C9- 4FEE-9400-587995198D50/Documents/SingleViewCoreData.sqlite options:(null) ... 返回错误 Error Domain=NSCocoaErrorDomain 代码=134100“(空)”用户信息={元数据={ NSPersistenceFrameworkVersion = 640; NSStoreModelVersionHashes = { 要求 = ; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( “” ); NSStoreType = SQLite; NSStoreUUID = "2654FBD8-4C1B-4001-827A-5C9D471B19C4"; "_NSAutoVacuumLevel" = 2; }, reason=开店的型号和开店的型号不兼容} 用户信息字典 { 元数据 = { NSPersistenceFrameworkVersion = 640; NSStoreModelVersionHashes = { 要求 = ; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( “” ); NSStoreType = SQLite; NSStoreUUID = "2654FBD8-4C1B-4001-827A-5C9D471B19C4"; "_NSAutoVacuumLevel" = 2; }; reason = "开店的型号和开店的型号不兼容"; } 2016-01-07 10:53:22.683 ... [956:28002] 未解决的错误错误域 = YOUR_ERROR_DOMAIN Code=9999 "初始化应用程序保存数据失败" UserInfo={NSLocalizedDescription=初始化失败 应用程序保存的数据,NSUnderlyingError=0x7a632aa0 {Error 域=NSCocoaErrorDomain 代码=134100 "(null)" UserInfo={metadata={ NSPersistenceFrameworkVersion = 640; NSStoreModelVersionHashes = { 要求 = ; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( “” ); NSStoreType = SQLite; NSStoreUUID = "2654FBD8-4C1B-4001-827A-5C9D471B19C4"; "_NSAutoVacuumLevel" = 2; }, reason=开店的型号和开店的型号不兼容}}, NSLocalizedFailureReason=创建或加载 应用程序保存的数据。}, [NSLocalizedDescription: Failed to 初始化应用程序保存的数据,NSUnderlyingError: Error 域=NSCocoaErrorDomain 代码=134100 "(null)" UserInfo={metadata={ NSPersistenceFrameworkVersion = 640; NSStoreModelVersionHashes = { 要求 = ; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( “” ); NSStoreType = SQLite; NSStoreUUID = "2654FBD8-4C1B-4001-827A-5C9D471B19C4"; "_NSAutoVacuumLevel" = 2; }, reason=开店的型号和开店的型号不兼容}, NSLocalizedFailureReason:创建或加载 应用程序保存的数据。] (lldb)

【问题讨论】:

  • 该错误不是由该行代码引起的。它告诉你哪个方法导致了错误——addPersistentStoreWithType:SQLite configuration:URL:options:error:

标签: ios swift core-data auto-increment


【解决方案1】:

相信你指的是objectID in the NSManagedObject class

var objectID: NSManagedObjectID { get }

在你的情况下尝试:

if results.count > 0 {
        for item in results as! [NSManagedObject]{
            let xml = item.valueForKey("xml")
            let statutenvoie = item.valueForKey("statutenvoie")

            print(item.objectID)
            print(xml!, statutenvoie!)
        }
    }

【讨论】:

  • @Ben 在修改模型时看起来像是一个错误。停止应用程序,从模拟器中删除它,清理项目,构建它并再次运行。
猜你喜欢
  • 2011-09-16
  • 1970-01-01
  • 2014-08-15
  • 2018-08-06
  • 2010-10-04
  • 2015-03-10
  • 1970-01-01
  • 1970-01-01
  • 2012-03-04
相关资源
最近更新 更多