2016-01-07 58 views
3

我發現一個主題,他們是一些名爲objectID來獲取表的每一行的自動遞增ID,我想得到它並顯示它,但我不明白怎麼做,我的代碼:核心數據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)

+0

該錯誤不引起該行代碼。它告訴你哪個方法導致了錯誤 - 'addPersistentStoreWithType:SQLite的配置:URL:選項:錯誤:' –

回答

0

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

看我的編輯,我得到一個錯誤 – Ben

+0

@Ben這看起來像一個錯誤時,修改模型。停止應用程序,從模擬器中刪除它,清理項目,構建它並再次運行。 – Michal

+0

我仍然有同樣的錯誤 – Ben