Error: Unrecognised Selector when using class reference
Error: Unrecognised Selector when using class reference
我在名为 cloud
的 swift 文件中有一个 class 我在我的项目中使用对那个 class
的引用来调用它的两个 方法。但是我收到以下错误并且应用程序崩溃了;
NSForwarding: warning: object 0x7fc938717160 of class 'App.Cloud' does
not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[App.Cloud persistentStoreWillChange]
(lldb)
有谁知道我可以解决这个问题以及为什么会这样?
PS: 该错误仅在安装应用程序并首次启动时出现。如果我退出并重新启动它不会出现,但它不会执行这些方法。
这是我的 class,
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)
}
}
Class 视图控制器中的引用:
let iCloudSync = Cloud()
override func viewWillAppear(animated: Bool) {
iCloudSync.addObsevers()
loadData()
}
override func viewWillDisappear(animated: Bool) {
iCloudSync.removeObservers()
}
好吧,你这样做,它说当通知触发时在 self
上调用 persistentStoreDidChange
:
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "persistentStoreDidChange",
name: NSPersistentStoreCoordinatorStoresDidChangeNotification,
object: nil)
但是你似乎没有在 class Cloud
中实现 persistentStoreDidChange
。
所以要么删除那个观察者,要么实现这个方法。
您的方法有参数,因此您必须在名称中用“:”表示。更改后的代码应如下所示:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("persistentStoreWillChange:"), name: NSPersistentStoreCoordinatorStoresWillChangeNotification, object: moc!.persistentStoreCoordinator)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("recieveICloudChanges:"), name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: moc!.persistentStoreCoordinator)
声明时需要从 NSObject 派生 class Cloud
,如下所示:
class Cloud : NSObject {
否则你的选择器对Objective-C是不可见的。
您更改了代码但忘记更改方法声明
func persistentStoreDidChange () {
println("Store Did Change")
}
至
func persistentStoreDidChange (notification: NSNotification) {
println("Store Did Change")
}
我在名为 cloud
的 swift 文件中有一个 class 我在我的项目中使用对那个 class
的引用来调用它的两个 方法。但是我收到以下错误并且应用程序崩溃了;
NSForwarding: warning: object 0x7fc938717160 of class 'App.Cloud' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[App.Cloud persistentStoreWillChange]
(lldb)
有谁知道我可以解决这个问题以及为什么会这样? PS: 该错误仅在安装应用程序并首次启动时出现。如果我退出并重新启动它不会出现,但它不会执行这些方法。
这是我的 class,
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)
}
}
Class 视图控制器中的引用:
let iCloudSync = Cloud()
override func viewWillAppear(animated: Bool) {
iCloudSync.addObsevers()
loadData()
}
override func viewWillDisappear(animated: Bool) {
iCloudSync.removeObservers()
}
好吧,你这样做,它说当通知触发时在 self
上调用 persistentStoreDidChange
:
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "persistentStoreDidChange",
name: NSPersistentStoreCoordinatorStoresDidChangeNotification,
object: nil)
但是你似乎没有在 class Cloud
中实现 persistentStoreDidChange
。
所以要么删除那个观察者,要么实现这个方法。
您的方法有参数,因此您必须在名称中用“:”表示。更改后的代码应如下所示:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("persistentStoreWillChange:"), name: NSPersistentStoreCoordinatorStoresWillChangeNotification, object: moc!.persistentStoreCoordinator)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("recieveICloudChanges:"), name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: moc!.persistentStoreCoordinator)
声明时需要从 NSObject 派生 class Cloud
,如下所示:
class Cloud : NSObject {
否则你的选择器对Objective-C是不可见的。
您更改了代码但忘记更改方法声明
func persistentStoreDidChange () {
println("Store Did Change")
}
至
func persistentStoreDidChange (notification: NSNotification) {
println("Store Did Change")
}