Removing subviews that match any given criteria!

So… suppose that you have a container view. And for some reason, you need to remove the subviews that match any given criteria.

The straightforward solution would be to write a foreach loop, by hand, and remove the target subviews with a method call. Guess what!. There is a kung fu solution to this..!!. Check this out:

[cc lang=”objc”]
NSPredicate* predicate = [NSPredicate predicateWithFormat:@”self isKindOfClass: %@”, [SomeView class]];
NSArray* viewsToRemove = [[self subviews] filteredArrayUsingPredicate:predicate];
[viewsToRemove makeObjectsPerformSelector:@selector(removeFromSuperview)];

Less code is better. Always.