Declared in NSDictionary+BlocksKit.h

Overview

Block extension for NSDictionary.

Both inspired by and resembling Smalltalk syntax, this utility allows iteration of a dictionary in a concise way that saves quite a bit of boilerplate code.

Includes code by the following:

Tasks

  • – bk_each:

    Loops through the dictionary and executes the given block using each item.

  • – bk_apply:

    Enumerates through the dictionary concurrently and executes the given block once for each pair.

  • – bk_match:

    Loops through a dictionary to find the first key/value pair matching the block.

  • – bk_select:

    Loops through a dictionary to find the key/value pairs matching the block.

  • – bk_reject:

    Loops through a dictionary to find the key/value pairs not matching the block.

  • – bk_map:

    Call the block once for each object and create a dictionary with the same keys and a new set of values.

  • – bk_any:

    Loops through a dictionary to find whether any key/value pair matches the block.

  • – bk_none:

    Loops through a dictionary to find whether no key/value pairs match the block.

  • – bk_all:

    Loops through a dictionary to find whether all key/value pairs match the block.

Instance Methods

bk_all:

- (BOOL)bk_all:(BOOL ( ^ ) ( id key , id obj ))block
Discussion

Loops through a dictionary to find whether all key/value pairs match the block.

Parameters

block

A two-argument, BOOL-returning code block.

Return Value

YES if the block returns YES for all key/value pairs in the dictionary, NO otherwise.

Declared In

NSDictionary+BlocksKit.h

bk_any:

- (BOOL)bk_any:(BOOL ( ^ ) ( id key , id obj ))block
Discussion

Loops through a dictionary to find whether any key/value pair matches the block.

This method is similar to the Scala list exists. It is functionally identical to bk_match: but returns a BOOL instead. It is not recommended to use bk_any: as a check condition before executing bk_match:, since it would require two loops through the dictionary.

Parameters

block

A two-argument, BOOL-returning code block.

Return Value

YES for the first time the block returns YES for a key/value pair, NO otherwise.

Declared In

NSDictionary+BlocksKit.h

bk_apply:

- (void)bk_apply:(void ( ^ ) ( id key , id obj ))block
Discussion

Enumerates through the dictionary concurrently and executes the given block once for each pair.

Enumeration will occur on appropriate background queues; the system will spawn threads as need for execution. This will have a noticeable speed increase, especially on dual-core devices, but you must be aware of the thread safety of the objects you message from within the block.

Parameters

block

A block that performs an action using a key/value pair.

Declared In

NSDictionary+BlocksKit.h

bk_each:

- (void)bk_each:(void ( ^ ) ( id key , id obj ))block
Discussion

Loops through the dictionary and executes the given block using each item.

Parameters

block

A block that performs an action using a key/value pair.

Declared In

NSDictionary+BlocksKit.h

bk_map:

- (NSDictionary *)bk_map:(id ( ^ ) ( id key , id obj ))block
Discussion

Call the block once for each object and create a dictionary with the same keys and a new set of values.

Parameters

block

A block that returns a new value for a key/value pair.

Return Value

Returns a dictionary of the objects returned by the block.

Declared In

NSDictionary+BlocksKit.h

bk_match:

- (id)bk_match:(BOOL ( ^ ) ( id key , id obj ))block
Discussion

Loops through a dictionary to find the first key/value pair matching the block.

bk_match: is functionally identical to bk_select:, but will stop and return the value on the first match.

Parameters

block

A BOOL-returning code block for a key/value pair.

Return Value

The value of the first pair found;

Declared In

NSDictionary+BlocksKit.h

bk_none:

- (BOOL)bk_none:(BOOL ( ^ ) ( id key , id obj ))block
Discussion

Loops through a dictionary to find whether no key/value pairs match the block.

This selector performs literally the exact same function as bk_all: but in reverse.

Parameters

block

A two-argument, BOOL-returning code block.

Return Value

YES if the block returns NO for all key/value pairs in the dictionary, NO otherwise.

Declared In

NSDictionary+BlocksKit.h

bk_reject:

- (NSDictionary *)bk_reject:(BOOL ( ^ ) ( id key , id obj ))block
Discussion

Loops through a dictionary to find the key/value pairs not matching the block.

This selector performs literally the exact same function as bk_select: but in reverse.

This is useful, as one may expect, for filtering objects. NSDictionary *strings = [userData bk_reject:^BOOL(id key, id value) { return ([obj isKindOfClass:[NSString class]]); }];

Parameters

block

A BOOL-returning code block for a key/value pair.

Return Value

Returns a dictionary of all objects not found.

Declared In

NSDictionary+BlocksKit.h

bk_select:

- (NSDictionary *)bk_select:(BOOL ( ^ ) ( id key , id obj ))block
Discussion

Loops through a dictionary to find the key/value pairs matching the block.

Parameters

block

A BOOL-returning code block for a key/value pair.

Return Value

Returns a dictionary of the objects found.

Declared In

NSDictionary+BlocksKit.h