为什么Swift闭包变量类型不能被隐式解包的选项?

时间:2015-09-24 04:45:53

标签: ios swift closures uialertview

例如,

alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:{ (x:UIAlertAction) in
...

会出现“找不到初始值设定项...”错误。

但如果我说UIAlertAction!UIAlertAction?而不只是UIAlertAction,那就行了。为什么是这样?

1 个答案:

答案 0 :(得分:0)

The types of these variables are all declared already in the Cocoa API. You must match them. A thing and an Optional wrapping that thing are not a match; they are two completely different types.

On the other hand, when types are known you can just omit them. So the easiest thing is for you to change x:UIAlertAction to simple x or even _.

相关问题