简体   繁体   English

比较两个viewController swift

[英]Compare two viewControllers swift

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 它们是相同的ViewController,但是末尾的数字不同,所以我认为这就是if(viewController == newController)为false的原因

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. 假设您的视图控制器是ViewController类的实例。 Then what you want to know is whether viewController is ViewController . 然后,您想知道的是viewController is ViewController是否viewController is ViewController Just like asking "is this a chair?" 就像问“这是椅子吗?”

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. 它们可能是或可能不是相同类型的视图控制器(相同的类),但是函数instantiateViewController() 始终会创建一个全新的,在此刻之前不存在的独特视图控制器实例。 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. 如果不进行范围检查,则表达式navController.viewControllers[navController.viewControllers.count - 2]很危险。 If the navigation controller only contains 1 view controller, it will crash with an index out of range error. 如果导航控制器仅包含1个视图控制器,它将崩溃,并出现索引超出范围错误。

您是否要比较两个视图控制器的Type

print(type(of: viewController) == type(of: newController))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM