34

In Swift, I often have to pass a noop closure to a method just to comply with the method's expected parameters (arity). In the good old days of Obj C, one could pass nil for a noop callback and be done with it. Is there a quicker more elegant way to do that in Swift without passing an empty block like below?

UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in }) // do nothing in callback

Complete example:

import UIKit

class UIAlertControllerFactory {
    class func ok(title: String, message: String) -> UIAlertController {
        var alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
        var okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in
        })
        alertController.addAction(okAction)
        return alertController
    }
}

4 Answers 4

55

In this case, you can pass nil, because the handler is an Optional:

var okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)

The general way, if you can't pass nil:

var okAction = UIAlertAction(title: "Ok", style: .default, handler: { _ in })
3
  • I swore nil didn't work in one of the betas. Thanks, I also like the _ approach.
    – dimroc
    Commented Oct 4, 2014 at 16:08
  • 2
    handler is of type ((UIAlertAction!) -> Void)!, what means that it is an Implicitly Unwrapped Optional. That why you can pass nil for this parameter. It is documented (see my answer for more details). Commented Oct 4, 2014 at 17:22
  • A ! suffix means an ImplicitlyUnwrappedOptional, which Apple mostly uses (in APIs) to mean that they haven't checked whether nil is legal or not. In iOS 9.0, they changed the type to ((UIAlertAction) -> Void)?, which means they verified that nil is allowed.
    – rob mayoff
    Commented Feb 20, 2016 at 19:48
10

You could define a noop to be used as a default closure that does nothing: (Reference: https://realm.io/news/closures-api-design)

func noop() {}

func noop<T>(value: T) {}

func noop<T>() -> T? { return nil }

func noop<T, S>(value: T) -> S? { return nil }

To use:

var okAction = UIAlertAction(title: "Ok", 
                             style: UIAlertActionStyle.Default, 
                           handler: noop)
1
  • Handy for cases where a parameter that takes a closure isn't nullable. (The UIAlertAction case in the question is nullable, though.) You might also need a few other variants for the APIs that don't take @convention(block) closures.
    – rickster
    Commented Dec 23, 2015 at 5:12
5

According to Xcode documentation, UIAlertAction has a convenience init method with the following declaration:

convenience init(title: String, style: UIAlertActionStyle, handler: ((UIAlertAction!) -> Void)!) 

As you can see, the handler parameter is an Implicitly Unwrapped Optional of type ((UIAlertAction!) -> Void)!. So you may pass nil for it. For example, you can create an UIAlertController instance that contains one UIAlertAction with handler: nil:

let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
presentViewController(alertController, animated: true, completion: nil)

Therefore, you don't need to create an empty block for handler.

3
extension UIAlertAction {
    convenience init(title: String, style: UIAlertActionStyle) {
        return self.init(title: title, style: style, handler:  { (UIAlertAction) -> Void in })
    }
}

and just leave out the last argument. You can put this into your "factory".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.