Implicitly Unwrapped Optionals in UIViewController init method

Wayne

As document said “you’re sure that the optional does contain a value, you can access its underlying value by adding an exclamation mark (!)

So why UIViewController init method use

init(nibName nibName: String!,bundle nibBundle: NSBundle!)

and tell me "If you specify nil, the nibName property is set to nil."

Why not use init(nibName nibName: String?,bundle nibBundle: NSBundle?) instead?

I am so confused about this.

smileBot
init(nibName nibName: String!,bundle nibBundle: NSBundle!)

Is calling into Objective C framework code. This calls initWithNibName:bundle. You can optionally pass this method's arguments nil. Objective C is fine with that. If you pass nil in initiWithNibName it tries to resolve the name for you based on the name of the class in question. If you pass in nil for bundle it replaces this with [NSBundle mainBundle].

So, Swift makes this parameter an optional. It must do so to permit the variable to optionally be nil. The question is why does it not make it a standard optional and require you to explicitly unwrap it. The answer is that the implicitly unwrapped optional immediately unwraps the optional before passing it to Objective C. You don't want to have to explicitly unwrap this since once you pass your argument in, whether nil or otherwise, you are done setting it. You don't want to have to explicitly unwrap it and then pass it to Objective C! Doing so wouldn't make any sense.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

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

From Java

Swift 3 incorrect string interpolation with implicitly unwrapped Optionals

From Dev

Implicitly Unwrapped Optionals in UIViewController init method

From Dev

How to use Implicitly Unwrapped Optionals?

From Dev

Implicitly unwrapped optional made immutable

From Dev

Optional Binding on Implicitly Unwrapped Optional

From Dev

Implicitly Unwrapped Optionals in Initialization - Swift

From Dev

Should I choose optionals or implicitly unwrapped optionals

From Dev

Returning an implicitly unwrapped optional

From Dev

Implicitly unwrapped multiplication and division

From Dev

Implicitly Unwrapped Optionals in Swift does not seem to work

From Dev

Implicitly unwrapped optional Closure in a method argument

From Dev

Implicitly Unwrapped Optionals and println

From Dev

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

From Dev

Incrementing an implicitly unwrapped optional

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

Comparing array of force unwrapped optionals

From Dev

Is there a technical downside to using implicitly-unwrapped optionals but testing for nil vs optional binding?

From Dev

Difference between Force Unwrapping Optionals and Implicitly Unwrapped Optionals

From Dev

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

From Dev

How to use Implicitly Unwrapped Optionals?

From Dev

Implicitly Unwrapped Optionals in Initialization - Swift

From Dev

Returning an implicitly unwrapped optional

From Dev

Implicitly unwrapped optional Closure in a method argument

From Dev

Implicitly Unwrapped Optionals and println

From Dev

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

From Dev

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

From Dev

Implicitly unwrapped optional from init!() in Swift 3.1

Related Related

  1. 1

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

  2. 2

    Swift 3 incorrect string interpolation with implicitly unwrapped Optionals

  3. 3

    Implicitly Unwrapped Optionals in UIViewController init method

  4. 4

    How to use Implicitly Unwrapped Optionals?

  5. 5

    Implicitly unwrapped optional made immutable

  6. 6

    Optional Binding on Implicitly Unwrapped Optional

  7. 7

    Implicitly Unwrapped Optionals in Initialization - Swift

  8. 8

    Should I choose optionals or implicitly unwrapped optionals

  9. 9

    Returning an implicitly unwrapped optional

  10. 10

    Implicitly unwrapped multiplication and division

  11. 11

    Implicitly Unwrapped Optionals in Swift does not seem to work

  12. 12

    Implicitly unwrapped optional Closure in a method argument

  13. 13

    Implicitly Unwrapped Optionals and println

  14. 14

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

  15. 15

    Incrementing an implicitly unwrapped optional

  16. 16

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

  17. 17

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

  18. 18

    Comparing array of force unwrapped optionals

  19. 19

    Is there a technical downside to using implicitly-unwrapped optionals but testing for nil vs optional binding?

  20. 20

    Difference between Force Unwrapping Optionals and Implicitly Unwrapped Optionals

  21. 21

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

  22. 22

    How to use Implicitly Unwrapped Optionals?

  23. 23

    Implicitly Unwrapped Optionals in Initialization - Swift

  24. 24

    Returning an implicitly unwrapped optional

  25. 25

    Implicitly unwrapped optional Closure in a method argument

  26. 26

    Implicitly Unwrapped Optionals and println

  27. 27

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

  28. 28

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

  29. 29

    Implicitly unwrapped optional from init!() in Swift 3.1

HotTag

Archive