我在一个名为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)
}
}
View Controller中的类引用:
let iCloudSync = Cloud()
override func viewWillAppear(animated: Bool) {
iCloudSync.addObsevers()
loadData()
}
override func viewWillDisappear(animated: Bool) {
iCloudSync.removeObservers()
}
答案 0 :(得分:1)
当您声明它时,需要从NSObject派生dynamic_cast
,如下所示:
class Cloud
否则您的选择器对于Objective-C是不可见的。
答案 1 :(得分:0)
嗯,你执行此操作,即在通知触发时在persistentStoreDidChange
上致电self
:
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "persistentStoreDidChange",
name: NSPersistentStoreCoordinatorStoresDidChangeNotification,
object: nil)
但是,您似乎未在persistentStoreDidChange
内实施class Cloud
。
所以要么删除那个观察者,要么实现该方法。
答案 2 :(得分:0)
您的方法有参数,因此您必须在其名称中用“:”表示。更改的代码应如下所示:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("persistentStoreWillChange:"), name: NSPersistentStoreCoordinatorStoresWillChangeNotification, object: moc!.persistentStoreCoordinator)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("recieveICloudChanges:"), name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: moc!.persistentStoreCoordinator)
答案 3 :(得分:0)
您更改了代码但忘记更改方法声明
func persistentStoreDidChange () {
println("Store Did Change")
}
到
func persistentStoreDidChange (notification: NSNotification) {
println("Store Did Change")
}