我一直在尝试创建一个UIAlertAtion
也有处理程序的。我阅读了这个问题的答案,并且知道该怎么做。
我的问题只是关于它的关闭部分。
1)我知道我会写:{alert in println("Foo")}
或者{_ in println("Foo")}
但我不会写{println("Foo")}
。在这里的评论中进行了解释,因为您需要处理参数 action。
这是否意味着由于处理程序的类型是(UIAlertAction) -> Void)?
我必须始终捕获传递的警报动作?
2)
我也读了这个,答案基本上是说你可以传入一个函数作为你的参数,但函数应该采用 type 的东西UIAlertAction -> Void
,我写了:
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) 在评论中还说:但除此之外,您不必快速编写 UIAlertActionStyle.Default 。.Default 也可以
我尝试不使用样式编写,因此默认为.Default
let sendLogAction = UIAlertAction(title: "Log") { action in print("goodbye")}
但后来我收到以下错误:
'(title: String, (_) -> ())' (aka '(title: String, _ -> ())') 不能转换为 '(title: String?, style: UIAlertActionStyle, handler: (( UIAlertAction) -> Void)?)' (aka '(title: Optional, style: UIAlertActionStyle, handler: Optional ()>)'),元组有不同数量的元素
4)
同时阅读这个答案。我不明白为什么我们需要传递alert
它是没有意义的。并不是不知道 alert 的类型是什么……我们不是已经定义了它的类型了吗?!!谁能解释一下传递动作本身通常在哪里有用,我的意思是我们能用它做什么?
我知道这是写成 4 个问题,但我认为这实际上只是一个基础问题。我已经广泛阅读,在我正在操场上工作和玩耍的项目中使用了闭包/完成处理程序,但我仍然感到困惑。