我已經有一個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 感謝您的幫助......
寫一個'handleStoresDidChange'函數肯定是一個好的開始,這是異常告訴你的。 – pNre 2015-01-21 08:52:24
@pNre:嗨。當我禁用這行時,應用程序是工作(沒有錯誤)。但我無法將我的coredata保存到icloud – 2015-01-21 09:00:32