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 about the closure portion of it.
1) I know I can write : {alert in println("Foo")}
Or {_ in println("Foo")}
but I can't write {println("Foo")}
. In the comments here it is explained because you need to handle the argument action.
Does this mean that since the handler is of type (UIAlertAction) -> Void)?
I must always capture the passed alertAction?
2)
I also read this and the answer is basically saying you can pass in a function as your argument, but the function should take something of type UIAlertAction -> Void
, which I wrote :
private func anything(action : UIAlertAction) {
print("hello")
}
and then wrote my alertaction as such:
let anotherAction = UIAlertAction(title: "hi", style: UIAlertActionStyle.Default,
handler: anything(action)) // error: Use of unresolved identifier 'action'
confused why I get that error
3)
In the comments it also said: But in addition to that you don't have to write UIAlertActionStyle.Default in swift. .Default works, too
I tried writing not using the style so it would be defaulted to .Default
let sendLogAction = UIAlertAction(title: "Log") { action in print("goodbye")}
But then I get the following error:
'(title: String, (_) -> ())' (aka '(title: String, _ -> ())') is not
convertible to '(title: String?, style: UIAlertActionStyle, handler:
((UIAlertAction) -> Void)?)' (aka '(title: Optional, style:
UIAlertActionStyle, handler: Optional ()>)'), tuples
have a different number of elements
4)
Also reading this answer. I don't understand why we need to pass in alert
it makes no sense. It's not like we don't know what are alert's type is...haven't we already defined its type?!! Can anyone explain where passing the action itself would be useful in general, I mean what could we do with it?
I know this is wrote as 4 questions but I think it's really just a foundational question. I have extensively read, used closures/completion handlers in a project I'm working and played in playground but still I'm confused.
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.
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")
}
}