I am trying to look for instance of specific class in array of different classes (looking for index of specific ViewController in TabBarViewController)
I have a code that checks type using type(of: ) but it doesn't work:
func getTabIndex(_ rootClass: AnyClass) -> Int? {
if let tabVC = UIApplication.shared.delegate?.window?!.rootViewController as? UITabBarController {
for (index, vc) in tabVC.viewControllers!.enumerated() {
if type(of: vc) == rootClass {
return index
}
}
}
return nil
}
if type(of: vc) == rootClass
always returns false.
When I change if type(of: vc) == rootClass
to vc.isMember(of: rootClass)
it works fine.
I assume isMember(of: ) is obj-c method while type(of: ) is pure swift, but I don't see why it would make difference on UIViewController.
Could somebody explain why type(of: ) doesn't work in this case? In both cases, I am comparing exact types, not the subclasses. TabBar has instances of FooViewController and BarViewController. When I call getTabIndex(FooViewController.self)
I get nil when using type(of: ) and 0 when using isMember(of: )
EDIT: I am aware that tpye(of: ) checks for exact type while kind(of: ) checks for subclasses as well, but in my case, I was checking exact type, so type(of: ) should be working fine, hence the confusion.
When I printed result of type(of: ) it was exactly same as when I printed value of rootClass. I know this only means that their textual representation is same and underlying types might be different, but I don't understand why that would be case
EDIT2:
Original question used isKind(of:) method which was misleading, I changed it to isMember(of: ) to reflect more closely source of my confusion, that is why type(of: ) doesn't work when comparing exact types
This issue was cause by Apptimize SDK. They are doing some shady stuff with classes for their UI editor to work. This results in changing type metadata for some classes (views, view controllers) and makes type(of: )
return false even if comparing same types. isMember(of: )
works because it's obj-c method and compares type in different, less rigorous way.
I suspect this can happen with other 3rd party libraries that try to be too clever as well, but in all other cases, type(of: )
should be working without any issues
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments