2015-01-21 110 views
2

我已經有一個CoreData,那麼現在我想將數據保存到iCloud。將CoreData保存到iCloud(Swift)

在AppDelegate.swift

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = { 
     var coordinator: NSPersistentStoreCoordinator = 
     NSPersistentStoreCoordinator (managedObjectModel: self.managedObjectModel) 

     var storeURL = 
     self.applicationDocumentsDirectory 
      .URLByAppendingPathComponent("DeviceCoreData.sqlite") 

     var storeOptions = 
     [NSPersistentStoreUbiquitousContentNameKey : "MyDevices" 
      // NSPersistentStoreRebuildFromUbiquitousContentOption: @(true) 
     ] 

     // 
     self.registerCoordinatorForStoreNotifications (coordinator) 

     var error : NSError? = nil 
     var store : NSPersistentStore! = 
     coordinator.addPersistentStoreWithType (NSSQLiteStoreType, 
      configuration: nil, 
      URL: storeURL, 
      options: storeOptions, 
      error: &error) 

     if nil == store { 
      // handle error 
     } 

     return coordinator 
     }() 

func registerCoordinatorForStoreNotifications (coordinator : NSPersistentStoreCoordinator) { 
    let nc : NSNotificationCenter = NSNotificationCenter.defaultCenter(); 

    nc.addObserver(self, selector: "handleStoresWillChange:", 
     name: NSPersistentStoreCoordinatorStoresWillChangeNotification, 
     object: coordinator) 

    nc.addObserver(self, selector: "handleStoresDidChange:", 
     name: NSPersistentStoreCoordinatorStoresDidChangeNotification, 
     object: coordinator) 

    nc.addObserver(self, selector: "handleStoresWillRemove:", 
     name: NSPersistentStoreCoordinatorWillRemoveStoreNotification, 
     object: coordinator) 

    nc.addObserver(self, selector: "handleStoreChangedUbiquitousContent:", 
     name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, 
     object: coordinator) 
} 

但是,當我建立應用程序,我得到一個錯誤信息

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Ơn_Giời.AppDelegate handleStoresDidChange:]: unrecognized selector sent to instance 0x17668770'

有人能幫助我嗎?我在這裏堆疊3天。

而且

[NSPersistentStoreUbiquitousContentNameKey : "MyDevices"] 

此鍵是否正確?我cloudkit命名iCloud.MyDevices 感謝您的幫助......

+0

寫一個'handleStoresDidChange'函數肯定是一個好的開始,這是異常告訴你的。 – pNre 2015-01-21 08:52:24

+0

@pNre:嗨。當我禁用這行時,應用程序是工作(沒有錯誤)。但我無法將我的coredata保存到icloud – 2015-01-21 09:00:32

回答

5

這條線:

nc.addObserver(self, selector: "handleStoresDidChange:", 
    name: NSPersistentStoreCoordinatorStoresDidChangeNotification, 
    object: coordinator) 

說,當通知發佈,名爲handleStoresDidChange:方法應該叫,對象self(顯然在,應用程序的委託)

錯誤:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Ơn_Giời.AppDelegate handleStoresDidChange:]: unrecognized selector sent to instance 0x17668770' 

AppDelegate沒有名爲handleStoresDidChange:的方法。

這就是應用程序崩潰的原因:您已經指示通知中心調用一個不存在的方法。如果您告訴通知中心應該調用一個方法,則該方法必須存在。您需要創建該方法或告訴通知中心調用其他存在的方法。

+0

謝謝,我發現了這些問題。再次感謝 – 2015-01-22 08:43:37

+0

@ZoomNguyễn不要忘記接受答案,如果它解決了你的問題。 – Shmidt 2015-08-11 02:01:06

+0

@Tom Harrington你已經很好地解釋了這個錯誤。很有幫助。謝謝 – 2016-01-16 01:49:48