Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Present a ViewController inside guard statement swift

I am trying to present a viewcontroller in case, status (an Int?) is nil as follows:

    guard let statusCode = status else {
        DispatchQueue.main.async() {
            let initViewController = self.storyboard!.instantiateViewController(withIdentifier: "SignInViewController")
            self.present(initViewController, animated: false, completion: nil)
        }
            return
    }

I want to know if this is the best practice because return after presenting a viewcontroller doesn't make much of a sense but it is required in guard statement as guard statement must not fall through.

like image 452
sandpat Avatar asked May 10 '18 05:05

sandpat


1 Answers

To answer your question regarding guard statement and return keyword

func yourFunc() {
    guard let statusCode = status else {
      return DispatchQueue.main.async() { [weak self] _ in
        let initViewController = self?.storyboard!.instantiateViewController(withIdentifier: "SignInViewController")
        self?.present(initViewController!, animated: false, completion: nil)
    }
  }
}
like image 192
Mansi Avatar answered Sep 27 '22 23:09

Mansi