how to check if the class is created by user of it is a class provided in pobjective-c API

久未见 提交于 2020-01-06 08:11:28

问题


i have a user-defined class of type CircularDynamicUIView. it is in an array the encompass several views like 'buttonviews, uiimageviews,scrollviews and others. how to programmatically within a loop to detect this user-defined class. for example: using if-statement

how to check if this class is the class created or developed by the user and not the one that already created by objective-c.


回答1:


As @rmaddy noted in comments, you cannot explicitly differentiate between, for example, your custom CircularDynamicUIView subclass and a default UIView ... but you can evaluate in a specific order.

Example (using your previous question):

for (id uiComponent in uiviews) {

    if ([uiComponent isKindOfClass:[CircularDynamicUIView class]]) {
        NSLog(@"Yes, it is a CircularDynamicUIView");
    } else {
        NSLog(@"No, it is NOT a CircularDynamicUIView");
    }

    // if CircularDynamicUIView is a subclass descendant of UIView
    // this will also be true
    if ([uiComponent isKindOfClass:[UIView class]]) {
        NSLog(@"Yes, it is a UIView");
    } else {
        NSLog(@"No, it is NOT a UIView");
    }

}

Since both if conditions will be true, you would not want to evaluate the second if if the first test was true.

Again, based on your previous question, you could use this approach:

for (id uiComponent in uiviews) {
    CircularDynamicUIView *cdView;
    UIView *uiView;
    UIImageView *uiImageView;

    if ([uiComponent isKindOfClass:[CircularDynamicUIView class]]) {
        cdView = (CircularDynamicUIView *)uiComponent;
    } else if ([uiComponent isKindOfClass:[UIImageView class]]) {
        uiImageView = (UIImageView *)uiComponent;
    } else if ([uiComponent isKindOfClass:[UIView class]]) {
        uiView = (UIView *)uiComponent;
    }


    if (cdView) {
        // do what you want because it's a CircularDynamicUIView
        if ([cdView isHidden]) {
            // ...
        }
        // etc ...
    }
    if (uiImageView) {
        // do what you want because it's a UIImageView
        if ([uiImageView isHidden]) {
            // ...
        }
        // etc ...
    }
    if (uiView) {
        // do what you want because it's a UIView
        if ([uiView isHidden]) {
            // ...
        }
        // etc ...
    }
}


来源:https://stackoverflow.com/questions/58771956/how-to-check-if-the-class-is-created-by-user-of-it-is-a-class-provided-in-pobjec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!