5

I am dealing with the External Accessory Framework and here is my code for registering the notofication..

override func viewDidLoad() {
    super.viewDidLoad()

    EAAccessoryManager.sharedAccessoryManager().registerForLocalNotifications()

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

And here is my method handling 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 if i dont give any parameters inside the accessoryDidConnectNotify function the application works good proceeding with the alert view when I insert a MFi accessory..

(i.e)  func accessoryDidConnectNotify(){   // works fine (with no arguments)
                         } 

but i need the NSNotification object to be used inside my accessoryDidConnectNotify function to get the name of the accessory ...but if I add the NSNotification object the appliaction crashes on inserting a MFi accessory...

(i.e)   func accessoryDidConnectNotify(notification: NSNotification){
}  // crashes app (with arguments)

If someone also came across the problem...please do share

Legolas
  • 805
  • 1
  • 11
  • 24
  • 4
    Just change `accessoryDidConnectNotify ` to `accessoryDidConnectNotify:` – Dharmesh Kheni Jul 07 '15 at 10:50
  • 2
    Your function does have one argument, thus the selector name must be `accessoryDidConnectNotify:` as @DharmeshKheni wrote. Here's [more info](http://stackoverflow.com/a/24007718/581190). – zrzka Jul 07 '15 at 10:52

1 Answers1

4

If your method doesn't have any parameter then you can call it this way:

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

by using "accessoryDidConnectNotify".

So that you can use that method like:

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

     //Your code
} 

But if your method have parameters then you have to call it this way:

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

By using this "accessoryDidConnectNotify:". here you have to add :.

Now you can call your method with parameters this way:

func accessoryDidConnectNotify(notification: NSNotification){

    //Your code
} 
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165