365Cocoa

A snippet of Objective-C code per day, 365 days long.
Written by Pieter Omvlee, developer and owner of Bohemian Coding.

Apr 3

Day 36: -filterUsingBlock:

Today I’ll highlight a method on NSArray which I’ll be using in the next chapter on NSResponder action. It doesn’t need much further explanation but still I think it’s useful:

- (NSArray *)filterUsingBlock:(BOOL (^)(id object))block
{
  return [self filteredArrayUsingPredicate:
      [NSPredicate predicateWithBlock:
      ^(id evaluatedObject, NSDictionary *bindings) {
    return block(evaluatedObject);
  }]];
}

Excuse the formatting, but that’s what you get with a narrow page design and the long method names from Cocoa.