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")
}
}
rmaddy's answer is enough, but being the OP :) I find the root cause of my question :
My lack in understanding the parameter handler, handler is of type: (UIAlertAction) -> Void)?
ie a closure/function
The handler is only expecting a function name, once you provide that function, it will fill in the inputs of that function itself.
you only pass anything
as the handler ie function name.
UIAlertAction's internal implementation has some line like handler(action)
which would use anything
which is of type(UIAlertAction) -> Void)?
, inject action
(which is passed onto it) eventually doing anything(action)
.
That being said, your anything(input: UIAlertAction)
should be do something meaningful. (already discussed in the comments with rmaddy)
The other solution is to not provide a function, but instead use the captured action in the trailing closure and do whatever you like doing with it.
let retryAction = UIAlertAction(title: returnTitle(), style: UIAlertActionStyle.Default) { action in
switch action.title {
case "OK":
okFunc()
case "cancel":
cancelFunc()
default:
defaultFunc()
}
}
Such switching is only valuable if you are getting the code dynamically otherwise there is no added value switching for a tile you know it's value.
Yes, you must always handle the argument. It's part of the signature and can't be ignored. It's also special Swift syntax being able to drop the handler
parameter since it is the last parameter and it is a closure parameter.
Change anything(action)
to anything
just like in the example you link to.
You misunderstand. They are not saying you can drop the style parameter. They are saying you can drop the UIAlertActionStyle
from UIAlertActionStyle.Default
meaning you only need to pass .Default
as the argument to the style parameter.
You want an example of where the action
parameter to the handler is useful. There aren't too many uses really. The only properties are the title, style, and whether it's enabled or not. The latter is pointless because if the handler was called you know it was enabled. The only possible use is if the title or style need to be used in the handler and they weren't hard coded into the alert action when it was created. By accessing the title or style properties in the handler, you have easy access to the actual values used when the action was created.