Compare two viewControllers swift
Asked Answered
C

3

5

I need to compare if two views are the same, I am getting the views like this

let viewController = navController.viewControllers[navController.viewControllers.count - 2]
        let newController = self.storyboard!.instantiateViewController(withIdentifier: "Reservar")

when I print the value of the variables I get this

<Resto.ReservaViewController: 0x7fc9f0e4a860>
<Resto.ReservaViewController: 0x7fc9f0d074a0>

They are the same ViewController, but that number at the end is different, so I think that is why the if (viewController == newController) is false

Crowned answered 7/8, 2018 at 23:25 Comment(0)
F
6

With your code:

let viewController = navController.viewControllers[navController.viewControllers.count - 2]
let newController = self.storyboard!.instantiateViewController(withIdentifier: "Reservar")

You can be completely certain that the two view controllers are not the same object. They may or may not be the same type of view controller (the same class) but the function instantiateViewController() always creates a brand-new, never-existed-before-this-moment, unique instance of the view controller. It might be an identical twin to another instance, with all the same settings, but it is still a unique object.

Tell us more about what you are trying to do. Are you looking to see if the object from the navigation controller is the type you are expecting?

Then you could use code like this:

let viewController = navController.viewControllers[navController.viewControllers.count - 2]
if viewController is ReservaViewController {
   //code to operate on that type
} else {
   //Code for other types of view controller
}

Or

if let viewController = navController.viewControllers[navController.viewControllers.count - 2] as? ReservaViewController {
  //Code to operate on a ReservaViewController
} else {
  //Code to deal with a view controller that's NOT a ReservaViewController
}

EDIT:

The expression navController.viewControllers[navController.viewControllers.count - 2] is dangerous without range checking. If the navigation controller only contains 1 view controller, it will crash with an index out of range error.

Foret answered 8/8, 2018 at 0:31 Comment(0)
A
10

Do you want to compare the Type of the two view controllers?

print(type(of: viewController) == type(of: newController))
Aggregate answered 7/8, 2018 at 23:29 Comment(0)
H
7

Two chairs are both chairs but they are still not the same chair.

It's the same for classes and objects.

Let's say your view controller is an instance of the ViewController class. Then what you want to know is whether viewController is ViewController. Just like asking "is this a chair?"

Hundley answered 7/8, 2018 at 23:36 Comment(0)
F
6

With your code:

let viewController = navController.viewControllers[navController.viewControllers.count - 2]
let newController = self.storyboard!.instantiateViewController(withIdentifier: "Reservar")

You can be completely certain that the two view controllers are not the same object. They may or may not be the same type of view controller (the same class) but the function instantiateViewController() always creates a brand-new, never-existed-before-this-moment, unique instance of the view controller. It might be an identical twin to another instance, with all the same settings, but it is still a unique object.

Tell us more about what you are trying to do. Are you looking to see if the object from the navigation controller is the type you are expecting?

Then you could use code like this:

let viewController = navController.viewControllers[navController.viewControllers.count - 2]
if viewController is ReservaViewController {
   //code to operate on that type
} else {
   //Code for other types of view controller
}

Or

if let viewController = navController.viewControllers[navController.viewControllers.count - 2] as? ReservaViewController {
  //Code to operate on a ReservaViewController
} else {
  //Code to deal with a view controller that's NOT a ReservaViewController
}

EDIT:

The expression navController.viewControllers[navController.viewControllers.count - 2] is dangerous without range checking. If the navigation controller only contains 1 view controller, it will crash with an index out of range error.

Foret answered 8/8, 2018 at 0:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.