arun rajesh
arun rajesh

Reputation: 181

iOS Swift3 check nil value for ViewController Object

let viewControllers: [UIViewController] = self.navigationController!.viewControllers

for VC  in viewControllers  {            
    if (VC.isKind(of: HomeViewController.self)) {            
         bScreen = true
         self.navigationController?.popToViewController(VC, animated: true)
     }
}

if bScreen == false {
    let homeVC = HomeViewController()
    self.navigationController?.pushViewController(homeVC, animated: false)
}

I loop through navigation controller array to move to HomeViewController.above code is working fine.some times i am getting crash as “fatal error: unexpectedly found nil while unwrapping an Optional value”.I know the cause for this crash.Please help me how to check nil value for view controller object.any help will be appreciated.thanks in advance

Upvotes: 0

Views: 1805

Answers (6)

Josechristy Martin
Josechristy Martin

Reputation: 88

Based on your code, In the loop, if the navigation stack contains the respective view controller will be popped to the respective page. But the thing is if the same view controller is present two times, will lead to execute the loop for the same time. This may cause crash. So Add a break after the poptoviewcontroller will avoid this issue. Please check the below code, will help you.

 if (VC.isKind(of: HomeViewController.self)) {

                bScreen = true
                self.navigationController?.popToViewController(VC, animated: true)
                break
            }

Upvotes: 0

Anuraj
Anuraj

Reputation: 1240

This is better to use to if let / guard for an optional value to avoid crashing.

if let viewControllers: [UIViewController] = self.navigationController.viewControllers{

        for VC  in viewControllers  {

            if (VC.isKind(of: HomeViewController.self)) {

                bScreen = true
                self.navigationController?.popToViewController(VC, animated: true)
            }
        }

if bScreen == false
        {
            let homeVC = HomeViewController()
            self.navigationController?.pushViewController(homeVC, animated: false)
        }
}

Upvotes: 0

Parth Adroja
Parth Adroja

Reputation: 13514

Never use directly ! until you are damn sure that it will not be nil. Replace your code as below. You can use if let or guard let to unwrap optionals.

    if let viewControllers: [UIViewController] = self.navigationController?.viewControllers {
                for VC  in viewControllers  {

                    if (VC.isKind(of: ViewController.self)) {

                        bScreen = true
                        self.navigationController?.popToViewController(VC, animated: true)
                    }
                }

                if bScreen == false
                {
                    let homeVC = ViewController()
                    self.navigationController?.pushViewController(homeVC, animated: false)
                }
 } 
else {
     // IF VC is nil
}

Upvotes: 1

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

let getCurrentVCIndex = self.navigationController?.viewControllers.indexOf({ (viewController) -> Bool in

if let _ = viewController as? HomeViewController {
    return true
}
return false
})


if  getCurrentVCIndex
 {
let HomeVC = self.navigationController?.viewControllers[getCurrentVCIndex!] as! HomeViewController

self.navigationController?.popToViewController(HomeVC, animated: true)
}
else
  {
 // use push
}

or use like

if  let  HomeVC =  self.navigationController?.viewControllers.filter({$0 is HomeViewController}).first
{
self.navigationController?.popToViewController(HomeVC!, animated: true)
}else
{
  // use push
}

Upvotes: 1

Harshal Valanda
Harshal Valanda

Reputation: 5451

-- Swift 3 --

for vc in (self.navigationController?.viewControllers)! {
     if vc is HomeViewController {
        _ = self.navigationController?.popToViewController(vc, animated: true)
     }
}

Upvotes: 2

Brijesh Shiroya
Brijesh Shiroya

Reputation: 3383

Use this code. this is helpful for you.

let viewControllers: [UIViewController] = self.navigationController!.viewControllers
            for VC  in viewControllers  {
                if (VC.isKind(of: HomeViewController.self)) {
                    bScreen = true
                    self.navigationController?.popToViewController(VC, animated: true)
                    break;
                }
            }

            if bScreen == false
            {
                let homeVC = HomeViewController()
                self.navigationController?.pushViewController(homeVC, animated: false)
            }

Upvotes: 1

Related Questions