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: