Check if object is Class type

后端 未结 5 1908

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:49

    No problem here is how you do it:

    if([NSStringFromClass([obj class]) isEqualToString:@"Class"]){
        NSLog(@"It is type of Class");
    }
    

    Edit

    Or you can make your Class conform to a protocol: and check if obj that you get from the array conforms to this protocol like this:

    if([obj conformsToProtocol:@protocol(MyClassProtocol)])
    

    Edit

    Or you can check if what you get from the array is an NSObject complient

    if ([object conformsToProtocol:@protocol(NSObject)]) {
        // Do something
    }
    

提交回复
热议问题