How can I get the opposite value of a Bool in Swift?

前端 未结 3 927
予麋鹿
予麋鹿 2021-01-18 15:02

My specific case is I am trying to toggle the nav bar hidden and showing.

    let navHidden = !self.navigationController?.navigationBarHidden
    self.naviga         


        
3条回答
  •  别那么骄傲
    2021-01-18 15:27

    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.

提交回复
热议问题