difference between comparisons using type(of: ) and isMember(of: )

Lope

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

Lope

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Difference between nameof and typeof

From Dev

difference between typeof "andrei" and typeof "andrei".valueOf()

From Dev

What's the difference between these 2 comparisons?

From Dev

Any difference between those comparisons in python?

From Dev

How to tell the difference between typeof(int) == typeof(int?) in C#

From Dev

Difference between typeof(/\s/) and $.type(/\s/)

From Dev

Javascript difference between typeof undefined and void?

From Dev

Undefined variable in Javascript: difference between typeof and not exists

From Dev

What's the difference between single and double equal signs (=) in shell comparisons?

From Dev

Difference between `or` comparisons that repeat the variable vs. ones that don't

From Dev

Compare only pairwise comparisons BETWEEN two matrices using rcorr() in R

From Dev

all vs. all comparisons between two dataframes using apply

From Dev

Is there a notable difference between 'result is T' vs 'typeof(T).IsInstanceOfType(result)'?

From Dev

SQL Difference between using = and IN

From Dev

Difference between using and using = for types?

From Dev

Difference between 'using' and 'using namespace'

From Dev

What is the difference between "using = " and "using"?

From Dev

Is there a difference between using a[i] and *(a + i)?

From Java

Difference between using gradlew and gradle

From Dev

Difference between using define and variable

From Dev

Difference between dates using PowerShell

From Dev

Difference between using [] and list() in Python

From Dev

Difference between toString() method and using (+"")

From Dev

Using echo; What is difference between > and >>

From Dev

Difference between using UINavigationController and UINavigationBar

From Dev

The difference between "on" and just using "hover"

From Dev

Difference between dates using PowerShell

From Dev

What is the difference between using .newInstance() and not using?

From Dev

difference between numbers using or without using variable

Related Related

  1. 1

    Difference between nameof and typeof

  2. 2

    difference between typeof "andrei" and typeof "andrei".valueOf()

  3. 3

    What's the difference between these 2 comparisons?

  4. 4

    Any difference between those comparisons in python?

  5. 5

    How to tell the difference between typeof(int) == typeof(int?) in C#

  6. 6

    Difference between typeof(/\s/) and $.type(/\s/)

  7. 7

    Javascript difference between typeof undefined and void?

  8. 8

    Undefined variable in Javascript: difference between typeof and not exists

  9. 9

    What's the difference between single and double equal signs (=) in shell comparisons?

  10. 10

    Difference between `or` comparisons that repeat the variable vs. ones that don't

  11. 11

    Compare only pairwise comparisons BETWEEN two matrices using rcorr() in R

  12. 12

    all vs. all comparisons between two dataframes using apply

  13. 13

    Is there a notable difference between 'result is T' vs 'typeof(T).IsInstanceOfType(result)'?

  14. 14

    SQL Difference between using = and IN

  15. 15

    Difference between using and using = for types?

  16. 16

    Difference between 'using' and 'using namespace'

  17. 17

    What is the difference between "using = " and "using"?

  18. 18

    Is there a difference between using a[i] and *(a + i)?

  19. 19

    Difference between using gradlew and gradle

  20. 20

    Difference between using define and variable

  21. 21

    Difference between dates using PowerShell

  22. 22

    Difference between using [] and list() in Python

  23. 23

    Difference between toString() method and using (+"")

  24. 24

    Using echo; What is difference between > and >>

  25. 25

    Difference between using UINavigationController and UINavigationBar

  26. 26

    The difference between "on" and just using "hover"

  27. 27

    Difference between dates using PowerShell

  28. 28

    What is the difference between using .newInstance() and not using?

  29. 29

    difference between numbers using or without using variable

HotTag

Archive