I have been trying to create a UIAlertAtion
which also has a handler. I read the answers from this question and know how to do it.
My question is only a
1) UIAlerAction init method definition takes 3 argument and the last argument is (UIAlertAction -> Void)? which means you can have a ‘no-name’ function (aka closure) or a nil (specified by optional symbol ?). If you choose to not specify the last argument you can create UIAlertController like so
alert.addAction(UIAlertAction(title: "test", style: .default, handler: nil))
but if you want to specify the last argument (not nil) then you have to provide a closure that take UIAlertAction as an argument and return nothing (Void). Referring your link, he just use ‘alert’ constant that he already created for simplicity.
To answer your question ‘Does this mean….’, the answer is Yes, because of the definition of the 3rd argument: (UIAlertAction) -> Void)?
2) You get the error because you pass the argument (action). Try let
anotherAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,
handler: anything)
and you should not get error.
3) UIAlertAction has only (I believe) one init method so you need to provide all three argument to create it. see my 1) answer for style:.default. This is a new way of calling Swift Enums. By the way all enum has to be lowercase so .default not .Default`enter code here
alert.addAction(UIAlertAction(title: String?, style: UIAlertActionStyle, handler: ((UIAlertAction) -> Void))
4) As you can see from the answer you have the link to. You can use the constant 'alert' (UIAlertAction) to do whatever you want with it, like checking the style and do some stuff.
func anything(alert: UIAlertAction!) {
print("somehandler")
switch alert.title {
case "OK"?:
print("title is OK")
default:
print("title is not OK")
}
}