ios - 核心数据id自增

标签 ios swift core-data auto-increment

我找到了一个主题,其中有一些名为 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) ... returned error 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=The model used to open the store is incompatible with the one used to create the store} with userInfo dictionary { metadata = { NSPersistenceFrameworkVersion = 640; NSStoreModelVersionHashes = { Demandes = ; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( "" ); NSStoreType = SQLite; NSStoreUUID = "2654FBD8-4C1B-4001-827A-5C9D471B19C4"; "_NSAutoVacuumLevel" = 2; }; reason = "The model used to open the store is incompatible with the one used to create the store"; } 2016-01-07 10:53:22.683 ...[956:28002] Unresolved error Error Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's saved data" UserInfo={NSLocalizedDescription=Failed to initialize the application's saved data, 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=The model used to open the store is incompatible with the one used to create the store}}, NSLocalizedFailureReason=There was an error creating or loading the application's saved data.}, [NSLocalizedDescription: Failed to initialize the application's saved data, 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=The model used to open the store is incompatible with the one used to create the store}, NSLocalizedFailureReason: There was an error creating or loading the application's saved data.] (lldb)

最佳答案

我相信您指的是 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!)
        }
    }

关于ios - 核心数据id自增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34650456/

相关文章:

Swift AnyObject 变量,使用或链接向下转换(as)以访问不同类的属性

objective-c - iOS核心数据:how to use preloaded sqlite database which contains multiple tables

ios - 从 ViewController 调用时如何检索存储在单独类中的数据?

ios - iTunes软件服务认证错误域错误434

iOS Swift - 修改一个数组会修改另一个数组

ios - 将数据从结构数组传递到新的 TableView Controller

iphone - 使用谓词对核心数据进行排序以消除重复

objective-c - Swift 核心数据 - 具有不同结果的请求

iphone - 导出时转换在 AVMutableVideoComposition 中不起作用

ios - 在 iOS 8.x 中,字符在 WebVTT 中被破坏以用于字幕