简体   繁体   中英

How to refer to navigation controller in Swift?

I have created a UINavigationController object and set it as the window's rootViewController property. The rootViewController of the UINavigationController object is a class called UINavigationMenuViewController . If I want to navigate from UINavigationMenuViewController to UIUserProfileViewController for example, I can use:

navigationController!.pushViewController(userProfileVC, animated: true)

as well as

navigationController?.pushViewController(userProfileVC, animated: true)

The effect seems to be the same. I am wondering what's the difference. I would guess the second way is more secure, and in case I forget to embed the UINavigationMenuViewController object inside the UINavigationController , the app would not crash, comparing to the first case. I guess it's also called optionals chaining, I am just not quite sure as I am still learning Swift.

Please give me some advice.

In case of doubt, it's always safer favoring optional chaining rather than forced unwrapping, for the reason you mentioned: if the variable is nil, it will cause the app to crash.

There are some cases where a crash is a good debugging tool though. If having your navigation controller set to nil, you might want to consider that as a development mistake, so making the app crash would make the mistake more explicit.

Besides that, my recommendation is to always use optional chaining and/or optional binding , and limit usage of forced unwrapping to cases where:

  • you are sure an optional is not nil
  • you have just checked for not nil
  • (as mentioned above) you do want the app to crash if the optional is nil

In the first case you unwrap navigationController explicitly so navigationController IS a UINavigationMenuViewController type and has to exist (or else crash). In the second case navigationController is an optional type and doesn't have to exist. If it doesn't exist of course nothing will happen and no view will be presented.

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