Skip to content

Instantly share code, notes, and snippets.

@jakerockland
Last active March 18, 2017 13:12
Show Gist options
  • Save jakerockland/8c5635baf090b9b3c1868d7ece3cf5a6 to your computer and use it in GitHub Desktop.
Save jakerockland/8c5635baf090b9b3c1868d7ece3cf5a6 to your computer and use it in GitHub Desktop.
//
// UIAlertController+Action.swift
//
import UIKit
/**
Extension to `UIAlertController` to allow direct adding of `UIAlertAction` instances
*/
extension UIAlertController {
func addAction(title: String?, style: UIAlertActionStyle, handler: ((UIAlertAction) -> Void)?) {
let alertAction = UIAlertAction(title: title, style: style, handler: handler)
self.addAction(alertAction)
}
}
@jbastosneto
Copy link

Really nice extension, clean and simple to use. Thanks for sharing!

@regexident
Copy link

regexident commented Mar 12, 2017

How exactly does this improved upon just doing this though?

alertController.addAction(UIAlertAction(title: "Foo", style: .default) { action in
    // handle action
})

@jakerockland
Copy link
Author

@jbastosneto Glad that you find it useful! 😄

@jakerockland
Copy link
Author

@regexident There is no performance benefit or anything like that, I was previously using the implementation that you pointed out and found it to be a bit messy when being used frequently in big projects. This extension makes the usage a bit cleaner and also has the benefit of XCode's autocompletion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment