Check if object is Class type

后端 未结 5 1914

I have a method that receives a NSArray of Class objects and I need to check if they all are Class type generated with the code bellow

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 10:50

    If you need to verify if the object in the array is a Class object then you can verify if it responds to class methods.

    for ( id obj in arr ) {
        if (([obj respondsToSelector:@selector(isSubclassOfClass:)])
              && (obj == [NSObject class]) ) {
            NSLog(@"%@", obj);
        }
    }
    

    Once you know it is a Class object by verifying if it responds to isSubclassOfClass: then you can check for direct equality with [NSObject class].

提交回复
热议问题