Search code examples
iosswiftnotificationsparameter-passinguitapgesturerecognizer

When Declaring A Function For Tap Gestures & Notifications Are Parameters/Arguments Necessary?


I know how tap gestures and notifications work so that's not the question.

What I notice is I've seen some people use a parameter/argument for a tap gesture function or a notification function:

override viewDidLoad(){
   super.viewDidLoad()


   NotificationCenter.default.addObserver(self, selector: #selector(notifyMe), name: NSNotification.Name(rawValue: notificationKey), object: nil)

   let gesture = UITapGesture(target: self, action: #selector(tapMe))
   view.addGestureRecognizer(gesture)
}


@objc func tapMe(_ sender: UITapGestureRecognizer){
   // do something
}

@objc func notifyMe(_ notification: Notification){
   // do something
}

However I've used the functions both with and without a parameter/argument and they work fine:

@objc func tapMe(){
   // do something
}

@objc func notifyMe(){
   // do something
}

What's the purpose of using the parameters/arguments if they work without them?


Solution

  • The purpose is to be able to identify and distinguish the sender / the object and in case of the notication to be able to get additional information / data from the userInfo dictionary.