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