简体   繁体   中英

Why can't Swift closure variable types be implicitly unwrapped optionals?

For example,

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

will give a "Cannot find initializer with..." error.

But if I say UIAlertAction! or UIAlertAction? instead of just UIAlertAction , it works. Why is this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM