How to compare UIViewController in Swift 3?

11,552

Solution 1

If you want to compare to a particular view controller you have to compare their refererences.

Try this...

if(vc === viewController) )
{
    return (self.navigationController?.popToViewController(vc, animated: animated)?.last)!
}

Solution 2

for viewsController in arrViewControllers
{
    if(viewsController.isKind(of: YourControllerClassName.self)){
    }
}

Solution 3

Swift 4 Hope it will work for you

   extension UINavigationController {
  func myPopToViewController(viewController:UIViewController, animated:Bool) {
    var arrViewControllers:[UIViewController] = []
    arrViewControllers = self.viewControllers
    for vc:UIViewController in arrViewControllers {
      if(vc.isKind(of: viewController.classForCoder)){
         (self.popToViewController(vc, animated: animated))
      }
    }
  }

}

Solution 4

In swift, we use is instead of isKind(of:).

is is used to check the type of the object.

So you can use,

if(vc is UIViewController)

But I think here you are trying to match the 2 references of UIViewController.

So, you need to use === instead of is. This operator is used to match 2 references of same type.

if(vc === viewController)
Share:
11,552
Varun Naharia
Author by

Varun Naharia

I am Software Engineer, Codding is my passion and want to explore every new and old language and technology. C is my first and most favorite language. C has given me power to understand do work in other language as I work in C. Beside of my love to C. I enjoy reading and exploring security issue and also love to solve problem and challenges. I have knowledge C, Java, J2EE, Objective-C, PHP. I am #SOreadytohelp my developer brother worldwide and stackoverflow is the only place. I am loving it. :)

Updated on June 09, 2022

Comments

  • Varun Naharia
    Varun Naharia almost 2 years

    I am trying to compare to UIViewController in Swift 3 but there is some error

    extension UINavigationController
    {
        func myPopToViewController(viewController:UIViewController, animated:Bool) -> UIViewController? {
            var arrViewControllers:[UIViewController] = []
    
            arrViewControllers = self.viewControllers
            for vc:UIViewController in arrViewControllers {
                if(vc.isKind(of: viewController) ) // This Line gives me error
                {
                    return (self.navigationController?.popToViewController(vc, animated: animated)?.last)!
                }
    
            }
            return nil
        }
    
    }
    

    /Users/varunnaharia/Documents/Projects/appname/appname/Public/UINavigationController+Extra.swift:18:30: Cannot convert value of type 'UIViewController' to expected argument type 'AnyClass' (aka 'AnyObject.Type')

    and if try to use

    if(vc is viewController)
    

    It gives

    /Users/varunnaharia/Documents/Projects/appname/appname/Public/UINavigationController+Extra.swift:18:22: Use of undeclared type 'viewController'

    I am calling it through this

    self.navigationController?.popOrPopToViewController(viewController: MyUIViewController(), animated: false)
    
  • Varun Naharia
    Varun Naharia about 7 years
    exactly same error Users/varunnaharia/Documents/Projects/appname/appname/Public‌​/UINavigationControl‌​ler+Extra.swift:18:3‌​0: Cannot convert value of type 'UIViewController' to expected argument type 'AnyClass' (aka 'AnyObject.Type')
  • Varun Naharia
    Varun Naharia about 7 years
    Thanks for your explanation