核心数据ID自动递增

3

我发现有一个名为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: error: -addPersistentStoreWithType:SQLite configuration:(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 Code=134100 "(null)" UserInfo={metadata={NSPersistenceFrameworkVersion = 640; NSStoreModelVersionHashes = {Demandes = ;}; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = (""); NSStoreType = SQLite; NSStoreUUID = "2654FBD8-4C1B-4001-827A-5C9D471B19C4"; "_NSAutoVacuumLevel" = 2; }, reason=打开存储的模型与创建存储的模型不兼容} with userInfo dictionary { metadata = { NSPersistenceFrameworkVersion = 640; NSStoreModelVersionHashes = { Demandes = ; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = (""); NSStoreType = SQLite; NSStoreUUID = "2654FBD8-4C1B-4001-827A-5C9D471B19C4"; "_NSAutoVacuumLevel" = 2; }; reason = "打开存储的模型与创建存储的模型不兼容"; } 2016-01-07 10:53:22.683 ...[956:28002] 未解决的错误 Error Domain=YOUR_ERROR_DOMAIN Code=9999 "无法初始化应用程序的保存数据" UserInfo={NSLocalizedDescription=无法初始化应用程序的保存数据, NSUnderlyingError=0x7a632aa0 {Error Domain=NSCocoaErrorDomain Code=134100 "(null)" UserInfo={metadata={NSPersistenceFrameworkVersion = 640; NSStoreModelVersionHashes = {Demandes = ;}; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = (""); NSStoreType = SQLite; NSStoreUUID = "2654FBD8-4C1B-4001-827A-5C9D471B19C4"; "_NSAutoVacuumLevel" = 2; }, reason=打开存储的模型与创建存储的模型不兼容}}, NSLocalizedFailureReason=创建或加载应用程序的保存数据时出错。}, [NSLocalizedDescription: 无法初始化应用程序的保存数据, NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134100 "(null)" UserInfo={metadata={NSPersistenceFrameworkVersion = 640; NSStoreModelVersionHashes = {Demandes = ;}; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = (""); NSStoreType = SQLite; NSStoreUUID = "2654FBD8-4C1B-4001-827A-5C9D471B19C4"; "_NSAutoVacuumLevel" = 2; }, reason=打开存储的模型与创建存储的模型不兼容}, NSLocalizedFailureReason: 创建或加载应用程序的保存数据时出错。] (lldb)

那个错误不是由那行代码引起的。它告诉你哪个方法引起了错误-- addPersistentStoreWithType:SQLite configuration:URL:options:error: - Tom Harrington
1个回答

0

我相信你指的是NSManagedObject类中的objectID

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 看起来是在调整模型时出现了错误。停止应用程序,从模拟器中删除它,清理项目,重新构建并再次运行。 - Michal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接