usage of isMemberOfClass; returning false

前端 未结 3 1180
生来不讨喜
生来不讨喜 2021-01-16 10:23

In the code below I thought the second condition would be true, but it is turning out as false. Am I missing something? Please help me understand.

NSArray *a         


        
相关标签:
3条回答
  • 2021-01-16 11:11

    You should be using isKindOfClass. Refer this for the difference.

    0 讨论(0)
  • 2021-01-16 11:20

    This is the correct behavior. Try inspecting the actual class for that object:

    NSArray *array = [[NSArray alloc] init];
    NSLog(@"%@", NSStringFromClass([array class]));
    

    The output you get is something like:

    2013-02-15 23:42:31.272 Project[91998:c07] __NSArrayI
    

    So the actual class is __NSArrayI (a private subclass of NSArray), not NSArray itself. Typically, isKindOfClass: provides more useful results.

    0 讨论(0)
  • 2021-01-16 11:27

    NSArray is a class cluster. When you create an object of NSArray, internally it creates the object from its cluster. It adds simplicity to avoid creation of different type of objects depending upon the requirements.

    For such cases you should use the function isKindOfClass. It checks the completer hierarchy to identify the kind of object.

    0 讨论(0)
提交回复
热议问题