Present a ViewController inside guard statement swift
Asked Answered
R

2

5

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.

Ribose answered 10/5, 2018 at 5:38 Comment(5)
if status is integer, I hope it can be zero not nilGoldcrest
it's an optional integer.Ribose
I recommend, don't make optional Int, initialize it with zero elseGoldcrest
Thanks PPL for your comment, nil or zero is matter of choice I guess. I am more curious to know if we can present a view controller inside a guard statement and if it is the best way to do it.Ribose
please find my answer and take your decision to use guard or if letGoldcrest
H
4

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)
    }
  }
}
Halfmoon answered 10/5, 2018 at 5:57 Comment(4)
interesting! Thanks MansiRibose
I guess this answered your question @Josh :)Halfmoon
More or less Mansi, I have not tested it but it should work. Suppose we are on background thread. In that case, I will have to wrap guard inside DispatchQueue.main.async to be able to return like this. Now if I unwrap 'let statusCode = status' in guard statement, it won't be available below guard statement as guard is on the main thread. I don't want to do everything on main thread except UI changes.Ribose
Nice Mansi, just unwrap your optional initViewcontroller in self?.present statement :)Ribose
I
2

You can try out if statements

if let statusCode = status {
    //Success Block    
}
else{
    DispatchQueue.main.async() {
        let initViewController = self.storyboard!.instantiateViewController(withIdentifier: "SearchTabBarViewController")
        self.present(initViewController, animated: false, completion: nil)
    }
}

Many of us are familiar with Optional Binding and the “if let” syntax convention when unwrapping an optional value. The “if let” allows us to unwrap optional values safely only when there is a value, and if not, the code block will not run. Simply put, its focus is on the “true” condition when a value exists. Different from the “if let”, the “guard” statement makes early exits possible with an emphasis on negative cases with errors rather than on positive cases. This means we can test negative cases earlier by running the guard’s else statement if the condition is NOT met, rather than wait for nested conditions to pass first. read more with example

Ingaborg answered 10/5, 2018 at 5:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.