ios - How to refer to navigation controller in Swift? -
i have created uinavigationcontroller
object , set window's rootviewcontroller
property. rootviewcontroller
of uinavigationcontroller
object class called uinavigationmenuviewcontroller
. if want navigate uinavigationmenuviewcontroller
uiuserprofileviewcontroller
example, can use:
navigationcontroller!.pushviewcontroller(userprofilevc, animated: true)
as as
navigationcontroller?.pushviewcontroller(userprofilevc, animated: true)
the effect seems same. wondering what's difference. guess second way more secure, , in case forget embed uinavigationmenuviewcontroller
object inside uinavigationcontroller
, app not crash, comparing first case. guess it's called optionals chaining, not quite sure still learning swift.
please give me advice.
in case of doubt, it's safer favoring optional chaining rather forced unwrapping, reason mentioned: if variable nil, cause app crash.
there cases crash debugging tool though. if having navigation controller set nil, might want consider development mistake, making app crash make mistake more explicit.
besides that, recommendation always use optional chaining and/or optional binding, , limit usage of forced unwrapping cases where:
- you sure optional not nil
- you have checked not nil
- (as mentioned above) want app crash if optional nil
Comments
Post a Comment