【发布时间】:2015-08-11 21:06:32
【问题描述】:
我在名为 cloud 的 swift 文件中有一个类,我在我的项目中使用对 class 的引用来调用它的两个方法。但我收到以下错误并且应用程序崩溃;
NSForwarding: 警告: 'App.Cloud' 类的对象 0x7fc938717160 确实 不实现 methodSignatureForSelector: -- 前面的麻烦
无法识别的选择器 -[App.Cloud persistentStoreWillChange]
(lldb)
有谁知道我可以解决这个问题以及为什么会发生这种情况? PS:该错误仅在安装应用程序并首次启动时出现。如果我退出并重新启动它不会出现,但是它不会执行这些方法。
这是我的课,
class Cloud {
let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
func persistentStoreWillChange (notification:NSNotification) {
self.moc!.performBlock { () -> Void in
if self.moc!.hasChanges {
var error:NSError? = nil
self.moc!.save(&error)
if error != nil {
println("Save error: \(error)")
} else{
// drop any manged object refrences
self.moc!.reset()
}
}
}
}
func persistentStoreDidChange () {
println("Store Did Change")
}
//Refresh Data
func recieveChanges (notification:NSNotification){
self.moc!.performBlock { () -> Void in
self.moc!.mergeChangesFromContextDidSaveNotification(notification)
}
}
//View Will Appear
func addObsevers() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "persistentStoreDidChange:", name: NSPersistentStoreCoordinatorStoresDidChangeNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("persistentStoreWillChange:"), name:NSPersistentStoreCoordinatorStoresWillChangeNotification, object: moc!.persistentStoreCoordinator)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("recieveICloudChanges:"), name:NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: moc!.persistentStoreCoordinator)
}
//View Will Dissapear
func removeObservers() {
NSNotificationCenter.defaultCenter().removeObserver(self, name: NSPersistentStoreCoordinatorStoresDidChangeNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: NSPersistentStoreCoordinatorStoresWillChangeNotification, object: moc!.persistentStoreCoordinator)
NSNotificationCenter.defaultCenter().removeObserver(self, name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: moc!.persistentStoreCoordinator)
}
}
视图控制器中的类引用:
let iCloudSync = Cloud()
override func viewWillAppear(animated: Bool) {
iCloudSync.addObsevers()
loadData()
}
override func viewWillDisappear(animated: Bool) {
iCloudSync.removeObservers()
}
【问题讨论】:
-
不完全确定我在使用断点时得到混合结果,但看看我错过了以下行的错误;
Unrecognized selector -[Jottit.Cloud persistentStoreDidChange] -
对我的问题中的错误块引用进行了更改以获取完整的控制台输出错误。
标签: ios swift class icloud nsnotificationcenter