我有两个UIViewControllers,我需要比较,看看它们是否是UIViewController的相同子类

时间:2017-02-16 13:59:24

标签: swift class comparison

如何检查两个UIViewController子类对象是否具有相同的子类?

2 个答案:

答案 0 :(得分:2)

在Swift 3中,您可以使用类型(of:)方法比较对象的类型:

class VC1: UIViewController {

}

class VC2: UIViewController {

}

let vc1 = VC1()
let vc2 = VC2()

let typeComparisonResult = type(of: vc1) == type(of: vc2)

答案 1 :(得分:0)

Swift允许使用is关键字进行类比较。例如,如果class aclass b都是UIViewController的子类。然后,您可以使用is关键字确认这一点。

class a:UIViewController
{

}

class b:UIViewController
{

}

let instanceA:a = a()
let instanceB:b = b()

if a is UIViewController && b is UIViewController
{
    print("Both are subclasses of UIViewController")
}