Using NSPredicate to Find the Object of a Specific Class

NSPredicate is one of powerful classes that helps you a lot when you can use it proficiently. One usage I often use is to use NSPredicate to scan the array for a specific item of a specific class.

1
2
3
4
5
6
NSArray* vcs = self.navigationController.viewControllers;
NSPredicate* classPredicate = [NSPredicate predicateWithFormat:@"class = %@", [TargetViewController class]];
NSArray* filteredArray = [vcs filteredArrayUsingPredicate:classPredicate];
if ([filteredArray count] != 0) {
  ....
}

Comments