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

rb612

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?

matt

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Implicitly Unwrapped Optionals in Initialization - Swift

From Dev

Implicitly Unwrapped Optionals in Initialization - Swift

From Dev

Implicitly Unwrapped Optionals in Swift does not seem to work

From Dev

Why '+=' does not work for implicitly unwrapped optionals?

From Dev

Why do implicitly unwrapped optionals need to unwrapped again in conditionals?

From Dev

Implicitly Unwrapped Optionals and println

From Dev

Implicitly Unwrapped Optionals and println

From Java

Swift 3 incorrect string interpolation with implicitly unwrapped Optionals

From Dev

What's a concise way to safely deal with implicitly unwrapped optionals in Swift?

From Dev

How to use Implicitly Unwrapped Optionals?

From Dev

How to use Implicitly Unwrapped Optionals?

From Dev

Should I choose optionals or implicitly unwrapped optionals

From Dev

Why is a Swift implicitly unwrapped optional `nil`?

From Dev

Implicitly Unwrapped Optionals in UIViewController init method

From Dev

Implicitly Unwrapped Optionals in UIViewController init method

From Dev

Swift: Comparing Implicitly Unwrapped Optionals results in "unexpectedly found nil while unwrapping an Optional values"

From Dev

How is Swift's "implicitly unwrapped optionals" different from Java's "autoboxing" of integers?

From Java

Why create "Implicitly Unwrapped Optionals", since that implies you know there's a value?

From Dev

Difference between Force Unwrapping Optionals and Implicitly Unwrapped Optionals

From Dev

Implicitly unwrapped optional Closure in a method argument

From Dev

Implicitly unwrapped optional Closure in a method argument

From Dev

Why can't Python increment variable in closure?

From Dev

In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

From Dev

In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

From Dev

Implicitly Unwrapped Optional Types - Possible Typo?

From Dev

Swift unary operator with implicitly unwrapped optional

From Dev

Swift unary operator with implicitly unwrapped optional

From Dev

Implicitly unwrapped optional from init!() in Swift 3.1

From Dev

Why can't I access the local variable in closure

Related Related

  1. 1

    Implicitly Unwrapped Optionals in Initialization - Swift

  2. 2

    Implicitly Unwrapped Optionals in Initialization - Swift

  3. 3

    Implicitly Unwrapped Optionals in Swift does not seem to work

  4. 4

    Why '+=' does not work for implicitly unwrapped optionals?

  5. 5

    Why do implicitly unwrapped optionals need to unwrapped again in conditionals?

  6. 6

    Implicitly Unwrapped Optionals and println

  7. 7

    Implicitly Unwrapped Optionals and println

  8. 8

    Swift 3 incorrect string interpolation with implicitly unwrapped Optionals

  9. 9

    What's a concise way to safely deal with implicitly unwrapped optionals in Swift?

  10. 10

    How to use Implicitly Unwrapped Optionals?

  11. 11

    How to use Implicitly Unwrapped Optionals?

  12. 12

    Should I choose optionals or implicitly unwrapped optionals

  13. 13

    Why is a Swift implicitly unwrapped optional `nil`?

  14. 14

    Implicitly Unwrapped Optionals in UIViewController init method

  15. 15

    Implicitly Unwrapped Optionals in UIViewController init method

  16. 16

    Swift: Comparing Implicitly Unwrapped Optionals results in "unexpectedly found nil while unwrapping an Optional values"

  17. 17

    How is Swift's "implicitly unwrapped optionals" different from Java's "autoboxing" of integers?

  18. 18

    Why create "Implicitly Unwrapped Optionals", since that implies you know there's a value?

  19. 19

    Difference between Force Unwrapping Optionals and Implicitly Unwrapped Optionals

  20. 20

    Implicitly unwrapped optional Closure in a method argument

  21. 21

    Implicitly unwrapped optional Closure in a method argument

  22. 22

    Why can't Python increment variable in closure?

  23. 23

    In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

  24. 24

    In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

  25. 25

    Implicitly Unwrapped Optional Types - Possible Typo?

  26. 26

    Swift unary operator with implicitly unwrapped optional

  27. 27

    Swift unary operator with implicitly unwrapped optional

  28. 28

    Implicitly unwrapped optional from init!() in Swift 3.1

  29. 29

    Why can't I access the local variable in closure

HotTag

Archive