将 CoreData 保存到 iCloud (Swift)
Save CoreData to iCloud (Swift)
我已经有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)
}
但是当我构建应用程序时,我收到一条错误消息
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
感谢您的帮助...
这一行:
nc.addObserver(self, selector: "handleStoresDidChange:",
name: NSPersistentStoreCoordinatorStoresDidChangeNotification,
object: coordinator)
表示当发布该通知时,应该在对象 self
(显然是应用程序委托)
上调用名为 handleStoresDidChange:
的方法
错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Ơn_Giời.AppDelegate handleStoresDidChange:]: unrecognized selector sent to instance 0x17668770'
表示 AppDelegate
没有名为 handleStoresDidChange:
的方法。
这就是应用程序崩溃的原因:您已指示通知中心调用不存在的方法。如果你告诉通知中心它应该调用一个方法,那个方法必须存在。您需要创建该方法或告诉通知中心调用其他确实存在的方法。
我已经有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)
}
但是当我构建应用程序时,我收到一条错误消息
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 感谢您的帮助...
这一行:
nc.addObserver(self, selector: "handleStoresDidChange:",
name: NSPersistentStoreCoordinatorStoresDidChangeNotification,
object: coordinator)
表示当发布该通知时,应该在对象 self
(显然是应用程序委托)
handleStoresDidChange:
的方法
错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Ơn_Giời.AppDelegate handleStoresDidChange:]: unrecognized selector sent to instance 0x17668770'
表示 AppDelegate
没有名为 handleStoresDidChange:
的方法。
这就是应用程序崩溃的原因:您已指示通知中心调用不存在的方法。如果你告诉通知中心它应该调用一个方法,那个方法必须存在。您需要创建该方法或告诉通知中心调用其他确实存在的方法。