Need help understanding this syntax

Copied the code below from a tutorial, and I mostly understand what is going on. But I'd like to be able to fully read it.

What I do get is that:

handler can be nil, but here it's the code to run upon the completion of UIAlertAction.

But am unsure what (_) in is. I have also sometimes seen it as [self] in

Thank you

controller.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_)  in controller.dismiss(animated: true, completion: nil)}))
Answered by Claude31 in 697760022

But am unsure what  (_) in is.

_ means that the var name is hidden.

. Let's explain:

you could write fully expanded syntax:

controller.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void  in controller.dismiss(animated: true, completion: nil)}))

This would give you a reference to the action: UIAlertAction parameter .

You can also skip -> Void (as you do when you declare a func without return value):

controller.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action)  in controller.dismiss(animated: true, completion: nil)}))

And finally, if you don't need access to action, you can replace with _

controller.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_)  in controller.dismiss(animated: true, completion: nil)}))

Going even further, you can remove the parenthesis:

controller.addAction(UIAlertAction(title: "OK", style: .default, handler: { _  in controller.dismiss(animated: true, completion: nil)}))

.

I have also sometimes seen it as [self] in

This "captures" self and avoid you to have to write self.someProperty each time you need to access inside the closure to someProperty or func defined of the class (self).

Accepted Answer

But am unsure what  (_) in is.

_ means that the var name is hidden.

. Let's explain:

you could write fully expanded syntax:

controller.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void  in controller.dismiss(animated: true, completion: nil)}))

This would give you a reference to the action: UIAlertAction parameter .

You can also skip -> Void (as you do when you declare a func without return value):

controller.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action)  in controller.dismiss(animated: true, completion: nil)}))

And finally, if you don't need access to action, you can replace with _

controller.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_)  in controller.dismiss(animated: true, completion: nil)}))

Going even further, you can remove the parenthesis:

controller.addAction(UIAlertAction(title: "OK", style: .default, handler: { _  in controller.dismiss(animated: true, completion: nil)}))

.

I have also sometimes seen it as [self] in

This "captures" self and avoid you to have to write self.someProperty each time you need to access inside the closure to someProperty or func defined of the class (self).

Need help understanding this syntax
 
 
Q