NSArray(SCBlocksAdditions) Category Reference
Declared in | NSArray+SCBlocksAdditions.h |
Overview
This category adds extensions that allow using the functional programming patterns in your applications.
The methods are built on top of the [NSArray enumerateObjectsUsingBlock:] method and are capable of solving the task of
- iterating
- filtering array contents
- transforming the array ( mapping )
Tasks
-
+ arrayWithSize:producer:
Creates a new NSArray of length N containing the values returned by the block.
-
– each:
Calls block once for each element in self, passing that element as a parameter.
-
– map:
Invokes block once for each element of self.
Creates a new NSArray containing the values returned by the block. -
– flatten:
Invokes the block passing in successive elements from self.
Creates a new NSArray containing all elements of all arrays returned the block. -
– select:
Invokes the block passing in successive elements from self.
Creates a new NSArray containing those elements for which the block returns a YES value. -
– count:
Invokes the block passing in successive elements from self, returning a count of those elements for which the block returns a YES value.
-
– firstMatch:
Invokes the block passing in successive elements from self, returning the first element for which the block returns a YES value.
-
– firstIndexOfObjectMatch:
Invokes the block passing in successive elements from self, returning an index of the first element for which the block returns a YES value.
-
– transformWithArray:withBlock:
Iterates two arrays simultaneously. Objects with same indices are passed to the block.
Class Methods
arrayWithSize:producer:
Creates a new NSArray of length N containing the values returned by the block.
+ (instancetype)arrayWithSize:(NSUInteger)N producer:(SCProducerBlock)block
Parameters
- N
- size of the new array
- block
- a block that builds new objects to fill array with
Return Value
- a new NSArray instance
Discussion
Creates a new NSArray of length N containing the values returned by the block.
Declared In
NSArray+SCBlocksAdditions.h
Instance Methods
count:
Invokes the block passing in successive elements from self, returning a count of those elements for which the block returns a YES value.
- (NSUInteger)count:(SCPredicateBlock)predicate
Parameters
- predicate
A predicate that takes an object and returns YES if the object matches a certain condition.
Return Value
number of objects matching the predicate
Discussion
Invokes the block passing in successive elements from self, returning a count of those elements for which the block returns a YES value.
Declared In
NSArray+SCBlocksAdditions.h
each:
Calls block once for each element in self, passing that element as a parameter.
- (void)each:(SCActionBlock)block
Parameters
- block
Performs some action with the given object. It is executed for each element of the array. The execution order is not guaranteed to be the same as the order of elements in the array.
Discussion
Calls block once for each element in self, passing that element as a parameter.
Declared In
NSArray+SCBlocksAdditions.h
firstIndexOfObjectMatch:
Invokes the block passing in successive elements from self, returning an index of the first element for which the block returns a YES value.
- (NSUInteger)firstIndexOfObjectMatch:(SCPredicateBlock)predicate
Parameters
- predicate
A predicate that takes an object and returns YES if the object matches a certain condition.
Return Value
index of the found object or NSNotFound otherwise
Discussion
Invokes the block passing in successive elements from self, returning an index of the first element for which the block returns a YES value.
Declared In
NSArray+SCBlocksAdditions.h
firstMatch:
Invokes the block passing in successive elements from self, returning the first element for which the block returns a YES value.
- (id)firstMatch:(SCPredicateBlock)predicate
Parameters
- predicate
A predicate that takes an object and returns YES if the object matches a certain condition.
Return Value
- the first object matching the predicate
Discussion
Invokes the block passing in successive elements from self, returning the first element for which the block returns a YES value.
Declared In
NSArray+SCBlocksAdditions.h
flatten:
Invokes the block passing in successive elements from self.
Creates a new NSArray containing all elements of all arrays returned the block.
- (NSArray *)flatten:(SCFlattenBlock)block
Parameters
- block
A block for retrieving an NSArray from the array element. All its elements will be appenden to the result array.
Return Value
A flat array.
For example,
NSArray tree = @[ @[ @1, @2 ], @[ @3, @4 ] ] ];
NSArray result = [ tree flatten: ^NSArray( id element )
{
return element;
} ];
// returns @[ @1, @2, @3, @4 ]
Discussion
Invokes the block passing in successive elements from self.
Creates a new NSArray containing all elements of all arrays returned the block.
Declared In
NSArray+SCBlocksAdditions.h
map:
Invokes block once for each element of self.
Creates a new NSArray containing the values returned by the block.
- (NSArray *)map:(SCMappingBlock)block
Parameters
- block
- a block that perfroms the transform of element in this array to element in the new array.
Return Value
a new NSArray of mapped objects
Discussion
Invokes block once for each element of self.
Creates a new NSArray containing the values returned by the block.
Declared In
NSArray+SCBlocksAdditions.h
select:
Invokes the block passing in successive elements from self.
Creates a new NSArray containing those elements for which the block returns a YES value.
- (NSArray *)select:(SCPredicateBlock)predicate
Parameters
- predicate
- a predicate. It takes an object and returns YES if the object matches a certain condition.
Return Value
- a new NSArray of objects that fit predicate requirements
Discussion
Invokes the block passing in successive elements from self.
Creates a new NSArray containing those elements for which the block returns a YES value.
Declared In
NSArray+SCBlocksAdditions.h
transformWithArray:withBlock:
Iterates two arrays simultaneously. Objects with same indices are passed to the block.
- (void)transformWithArray:(NSArray *)other withBlock:(SCTransformBlock)block
Parameters
- other
An array of the same size
- block
Performs some action with the given pair of objects. Both objects have the same index in the corresponding arrays ( “self” and “other” ).
Discussion
Iterates two arrays simultaneously. Objects with same indices are passed to the block.
Declared In
NSArray+SCBlocksAdditions.h