您应该如何处理UIAlertAction的闭包参数 [英] How should you handle closure arguments for UIAlertAction

查看:170
本文介绍了您应该如何处理UIAlertAction的闭包参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建一个具有处理程序的UIAlertAtion.我阅读了这个问题的答案,并且知道该怎么做.

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)我知道我可以写:{alert in println("Foo")}{_ in println("Foo")},但是我不能{println("Foo")}.在此处的注释中,解释了此原因,因为您需要处理自变量操作.

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.

这是否意味着由于处理程序的类型为(UIAlertAction) -> Void)?,我必须始终捕获传递的alertAction?

Does this mean that since the handler is of type (UIAlertAction) -> Void)? I must always capture the passed alertAction?

2) 我还阅读了,答案基本上是说您可以传入函数作为参数,但是该函数应该包含一些内容我写的是UIAlertAction -> Void类型的

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) 在注释中还说:但是除此之外,您不必快速编写UIAlertActionStyle.Default. .默认也可以

我尝试编写不使用样式的样式,因此它将默认设置为.Default

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:

'(标题:字符串,(_)->())'(aka'(标题:字符串,_->())')不是 可转换为'(title:String ?, style:UIAlertActionStyle,handler: (((UIAlertAction)-> Void)?)'(aka'(title:Optional,style: UIAlertActionStyle,处理程序:可选()>)'),元组 有不同数量的元素

'(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) 另请阅读答案.我不明白为什么我们需要传递alert这没有任何意义.好像我们不知道警报的类型是什么...我们是否已经定义了它的类型?!任何人都可以解释在一般情况下通过操作本身会在什么地方有用,我的意思是我们可以用它做什么?


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?

我知道这被写成4个问题,但我认为这实际上只是一个基本问题.我广泛地阅读,在我正在工作并在操场上玩过的项目中使用了闭包/补全处理程序但是我还是很困惑.

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.

推荐答案

  1. 是的,您必须始终处理该参数.它是签名的一部分,不能忽略.这也是一种特殊的Swift语法,能够删除handler参数,因为它是最后一个参数,并且是一个闭包参数.

  1. 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.

就像链接到的示例一样,将anything(action)更改为anything.

Change anything(action) to anything just like in the example you link to.

您误会了.他们并不是说您可以删除样式参数.他们说您可以从UIAlertActionStyle.Default中删除UIAlertActionStyle,这意味着您只需要将.Default作为参数传递给样式参数.

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.

您需要一个示例,说明处理程序的action参数在哪里有用.确实没有太多用途.唯一的属性是标题,样式以及是否启用.后者是没有意义的,因为如果调用了处理程序,您就知道它已启用.唯一可能的用途是在处理程序中需要使用标题或样式,并且在创建警报动作时并未将其硬编码到警报动作中.通过访问处理程序中的title或style属性,您可以轻松访问创建操作时使用的实际值.

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.

这篇关于您应该如何处理UIAlertAction的闭包参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆