Search code examples
iosswiftclassicloudnsnotificationcenter

Error: Unrecognised Selector when using class reference


I have a class in a swift file called cloud I use a reference to that class in my project to call two of its methods. But I get the following error and the app crashes;

NSForwarding: warning: object 0x7fc938717160 of class 'App.Cloud' does not implement methodSignatureForSelector: -- trouble ahead

Unrecognized selector -[App.Cloud persistentStoreWillChange]

(lldb)

Does anybody know who i could resolve this and why this is occurring ? PS: The error only appears when installing the app and launching for the first time. If I quit and relaunch it does not appear, however it does not execute the methods.

Heres my 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 reference in View Controller:

let iCloudSync = Cloud()

override func viewWillAppear(animated: Bool) {

    iCloudSync.addObsevers()

    loadData()

}

override func viewWillDisappear(animated: Bool) {
    iCloudSync.removeObservers()
}

Solution

  • You changed code but you forgot to change method declaration from

    func persistentStoreDidChange () {
        println("Store Did Change")
    }
    

    to

    func persistentStoreDidChange (notification: NSNotification) {
        println("Store Did Change")
    }