Iterate everything except UIContainerView

时间:2017-04-10 02:27:37

标签: ios objective-c uicontainerview

I have a custom class (that I get from SO) to iterate all and every single subclass of UIView. What I don't get is I cannot add code to exempt the UIContainerView.

The code (in UIView+Recursion.m):

- (NSMutableArray*)allSubViews
{
    NSMutableArray *arr= [[NSMutableArray alloc] init];
    [arr addObject:self];
    for (UIView *subview in self.subviews)
    {
        [arr addObjectsFromArray:(NSArray*)[subview allSubViews]];
    }
    return arr;
}

So I want to add in the for loop, if it is UIContainerView, then don't add to the array. What I tried:

- (NSMutableArray*)allSubViews
{
    NSMutableArray *arr= [[NSMutableArray alloc] init];
    [arr addObject:self];
    for (UIView *subview in self.subviews)
    {
        if (![subview isKindOfClass:[UIContainerView class]]) <-- error use of undeclared identifier
        [arr addObjectsFromArray:(NSArray*)[subview allSubViews]];
    }
    return arr;
}

Complete error message:

Use of undeclared identifier 'UIContainerView'

The category class seem to be not recognizing the UIContainerView?

1 个答案:

答案 0 :(得分:1)

It seems you wish to check for a private class which is why your code won't compile. If this is what you really need to do, then you can use NSClassFromString:

- (NSMutableArray *)allSubViews
{
    Class cvclass = NSClassFromString(@"UIContainerView");
    NSMutableArray *arr= [[NSMutableArray alloc] init];
    [arr addObject:self];
    for (UIView *subview in self.subviews)
    {
        if (![subview isKindOfClass:cvclass]) {
            [arr addObjectsFromArray:[subview allSubViews]];
        }
    }

    return arr;
}