The method handler for the received notification causes the application to crash. :(

I deal with the external infrastructure of accessories, and here is my code for registering inaccuracy.

override func viewDidLoad() { super.viewDidLoad() EAAccessoryManager.sharedAccessoryManager().registerForLocalNotifications() NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify", name: EAAccessoryDidConnectNotification, object: nil) } 

And here is my method processing function ...

 func accessoryDidConnectNotify(notification: NSNotification){ let alert : UIAlertController = UIAlertController(title: "Alert", message: "MFi Accessory Connected", preferredStyle:UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.Default, handler: { (action) -> Void in })) self.presentViewController(alert, animated: true, completion: nil) 

And my problem is that if I do not give any parameters inside the accessoriesDidConnectNotify function, the application works fine with a warning view when I insert an MFi accessory.

 (ie) func accessoryDidConnectNotify(){ // works fine (with no arguments) } 

but I need an NSNotification object that will be used inside my accessoriesDidConnectNotify function to get the name of the accessory ... but if I add an NSNotification object, the application crashes when I insert the MFi accessory ...

 (ie) func accessoryDidConnectNotify(notification: NSNotification){ } // crashes app (with arguments) 

If someone also encountered a problem ... please share

+5
source share
1 answer

If your method has no parameters, you can call it as follows:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify", name: EAAccessoryDidConnectNotification, object: nil) 

using "accessoryDidConnectNotify" .

So that you can use this method, for example:

 func accessoryDidConnectNotify(){ // works fine (with no arguments) //Your code } 

But if your method has parameters, you should call it like this:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "accessoryDidConnectNotify:", name: EAAccessoryDidConnectNotification, object: nil) 

Using "accessoryDidConnectNotify:" . here you must add :

Now you can call your method with parameters as follows:

 func accessoryDidConnectNotify(notification: NSNotification){ //Your code } 
+4
source

All Articles