我已经有一个CoreData,那么现在我想将数据保存到iCloud。
在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)
}
但是当我构建应用程序时,我收到一条错误消息
由于未捕获的异常而终止应用程序
'NSInvalidArgumentException',原因:'-[Ơn_Giời.AppDelegate
handleStoresDidChange:]:无法识别的选择器已发送到实例
0x17668770'
有人可以帮助我吗?我在这里堆放3天。
和
[NSPersistentStoreUbiquitousContentNameKey : "MyDevices"]
这个金钥正确与否?我的Cloudkit名为iCloud.MyDevices
谢谢你的帮助...
最佳答案
这行:
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:
的方法。这就是应用程序崩溃的原因:您已指示通知中心调用不存在的方法。如果告诉通知中心它应调用一个方法,则该方法必须存在。您需要创建该方法或告诉通知中心调用确实存在的其他方法。
关于ios - 将核心数据保存到iCloud(快速),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28062370/