What does "?" mean in navigationController?

Asked 2 years ago, Updated 2 years ago, 32 views

@objc private func DidTapRegister(){
    let vc = RegisterViewController()
    vc.title="Create Account"
    navigationController?pushViewController(vc, animated:true)

swift

2022-09-30 17:56

1 Answers

navigationController?pushViewController(vc, animated:true)

This is one of the Swift syntaxes called Optical Chaining.

Optical Chaining-The Swift Programming Language

Swift has a number of syntaxes that reduce the fairly complex processing of multiple lines to a single line if you try to write it as it is, and Optional Chaining is one of them.If you write one line above without using Optional chaining, this is what you will see.

 if navigationController!=nil{
    let navigation=navigationController!
    navigation.pushViewController(vc, animated:true)
}

If you are writing in Japanese, "If the navigationController property nil has a value of nil, Unwrap the navigationController and run the UINavigationController method pushControl>".


2022-09-30 17:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.