My specific case is I am trying to toggle the nav bar hidden and showing.
let navHidden = !self.navigationController?.navigationBarHidden
self.navigationController?.setNavigationBarHidden(navHidden!, animated: true)
Is not working for me like it normally would in Obj-C.
The exclamation point is on the wrong side of the boolean. The way you've written it would indicate that the boolean could be nil. You want !navHidden.
navHidden!
is to make sure this is not optional. !navHidden
is the correct way to do that.
From Apple's book .
Trying to use ! to access a non-existent optional value triggers a runtime error. Always make sure that an optional contains a non-nil value before using ! to force-unwrap its value.
navHidden is an optional. And you explictely unwrap that optional (which means you get a crash if navHidden is nil). Clearly something is wrong here. I suggest
if let navController = self.navigationController {
let navHidden = navController.navigationBarHidden
navController.setNavigationBarHidden (!navHidden, animated:true)
}
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.