English 中文(简体)
你们应如何处理“积极行动”的结束辩论
原标题:How should you handle closure arguments for UIAlertAction

我一直在尝试建立<条码> 查询<<> > 查询/编码>,该代码也配备了手稿。 我宣读了。 问题并知道如何做到这一点。

我只问关闭部分。

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.

这是否意味着,由于手稿是“条码”的类型(即“危险行动”)——“沃德”? 我必须始终抓住已经通过的警示行动吗?


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")
}

接着,我提醒大家注意:

let anotherAction = UIAlertAction(title: "hi", style: UIAlertActionStyle.Default,
 handler: anything(action)) // error: Use of unresolved identifier  action 

混淆了我为何得出这一错误


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

我试图写字不使用风格,因此将不成<代码>。 Default

let sendLogAction = UIAlertAction(title: "Log") { action in print("goodbye")}

但当时我有以下错误:

(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?


我知道这4个问题,但我认为这确实是一个基本问题。 我已广泛使用,在“Im”项目中使用了关闭/复合处理器,在游乐场工作,但我仍然混淆不清。

最佳回答
  1. 是的,你必须始终处理这一论点。 它属于签字的一部分,不容忽视。 该公司还特别快速星号能够降低<代码>handler<>/code>参数,因为它是最后的参数,是封闭参数。

  2. 更改<条码>任何编码(行动)至<条码> 就像你与你一样。

  3. 你会错觉。 他们不说你可以放弃风格的参数。 他们说,你可以从<条码> 查询标准/编码>中删除<条码>。 Default作为风格参数的论据。

  4. 您希望举一个例子,说明“<条码>行动>参数对手有用。 实际使用的数量很少。 唯一一种特性是所有权、风格,以及是否允许。 后者是毫无意义的,因为如果有人打电话给你的是能够知道的。 唯一可能的用法是,如果标题或风格需要在手里使用,而且当警报行动产生时,这些名称或风格就很难编码。 通过获得手工艺品的称谓或风格,你很容易地获得产生行动时使用的实际价值。

问题回答

rmaddy s answer is enough, but being the OP :) I find the root cause of my question :

我对参数手法缺乏理解,手稿是类型:(AlertAction)-> Void?,即封闭/运行

The handler is only expecting a function name, once you provide that function, it will fill in the inputs of that function itself.

页: 1

UIAlertAction s internal implementation has some line suchhandler(action) which will use anything 哪一种类型的<代码>(倡议行动)-> Void?, 注入<>>>>。 (通过后)最终采用<代码>任何thing(action)

尽管如此,您的<代码>任何编号(投入: UIAlertAction)都应有意义。 (在评论中已讨论过)


另一个解决办法是而不是<>>提供一种功能,而是在拖车关闭时使用captured<>m>的行动,并做你喜欢做的事情。

let retryAction = UIAlertAction(title: returnTitle(), style: UIAlertActionStyle.Default) { action in
switch action.title {
        case "OK":
        okFunc()
        case "cancel":
        cancelFunc()
        default:
        defaultFunc()
        }
      }

如果你拿到法典动力学-,那么这种开关只是很有价值的,否则就没有为你所知的那块tile子带来附加值。

  1. 方法定义有3个论点,最后的论点是(UIAlertAction -> Void?) 。 (用任择文号加以具体说明?)。 如果你选择不具体指明最后的论点,你可以创建<条形码>。

    alert.addAction(UIAlertAction(title: "test", style: .default, handler: nil))
    

    但是,如果你想具体说明最后的论点(而不是<代码>nil)。 之后,您必须提供一种封闭,以<条码> 查询/代码>为理由,不作回报(<条码>。 他在提到你的联系时,只是使用“alert”的不变性,即他已经为简单化创造了条件。

    回答您的问题。 这是否意味着......,答案是Yes,因为第3条论点的定义: (AlertAction) -> Void)?

  2. 你之所以有错误,是因为你通过这一论点(>:)。 引 闻

    anotherAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: anything)
    

    不应有错误。

  3. www.un.org/Depts/DGACM/index_french.htm 见我的1份关于<代码>的回答:default。 这是一种叫 Swift埃南的新方式。 采用何种方式,所有表格均须作以下分类:.default。 Default

    alert.addAction(UIAlertAction(title: String?, style: UIAlertActionStyle, handler: ((UIAlertAction) -> Void))
    
  4. 你从答复中可以看到,你与你有联系。 您可以使用恒定的<代码>alert (UIAlertAction)来做你们想要做的一切,如检查风格和做些什么。

    func anything(alert: UIAlertAction!) {
      print("somehandler")
      switch alert.title {
      case "OK"?:
        print("title is OK")
      default:
        print("title is not OK")
      }
    }
    




相关问题
Closure/scope JavaScript/jQuery

I m trying to group some exisiting top-level functions inside a closure (to avoid polluting the global namespace) but I m not quite getting it to work. First, all the JS works outside my anonymous ...

Nested Lambdas in Python

I m a beginning python programmer, and I d like someone to clarify the following behavior. I have the following code: env = lambda id: -1 def add(id, val, myenv): return lambda x: val if x == ...

closures mean fully type-safe criteria?

combining closures (FCM) and generics, would it be possible to have fully type-safe criteria. // The following works without a cast as Foo.id is a long field. List<Long> ids = session....

Is nested XMLHttpRequests with multiple closures a good idea?

I have a Greasemonkey script which operates on a search results page at a video site. The function of the script is to take a javascript link that opens a new window with a flash player, jump through ...

How|Where are closed-over variables stored?

This is a question based on the article "Closing over the loop variable considered harmful" by Eric Lippert. It is a good read, Eric explains why after this piece of code all funcs will return the ...

Scope with a self-invoking function in Javascript

Take below code iterates over 6 input buttons and attaches an onclick event to every button that alerts the index number of the respective iteration: for (var i = 1; i < 6; ++i) { var but = ...