How should you handle closure arguments for UIAlertAction

后端 未结 3 1725
轻奢々
轻奢々 2020-12-21 07:34

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

3条回答
  •  悲哀的现实
    2020-12-21 08:00

    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.

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

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

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

提交回复
热议问题