NSArray(Blocks)

@interface NSArray <__covariant T>
(Blocks) @end

A set of widely used functional extensions to NSArray.

  • Traverses the array and calls the combine block, returning a single value that is accumulated by the block.

    Declaration

    Objective-C

    - (nonnull id)reduce:(nonnull id)initial
                 combine:(nonnull id _Nonnull (^)(id _Nonnull, T _Nonnull))combine;

    Swift

    func reduce(_ initial: Any, combine: @escaping (Any, Any) -> Any) -> Any
  • Traverses the array and returns a new array composed only of those items for which the provided block returns YES.

    Declaration

    Objective-C

    - (nonnull NSArray *)filter:(nonnull BOOL (^)(T _Nonnull, NSUInteger,
                                                  BOOL *_Nonnull))block;

    Swift

    func filter(_ block: @escaping (Any, UInt, UnsafeMutablePointer<ObjCBool>) -> Bool) -> [Any]
  • Traverses the array and calls the provided block with each item, returning a new array composed of the items returned by the block.

    Declaration

    Objective-C

    - (nonnull NSArray *)map:(nonnull id _Nonnull (^)(T _Nonnull))block;

    Swift

    func map(_ block: @escaping (Any) -> Any) -> [Any]
  • Traverses the array and returns YES if the provided block returns YES for any item.

    Declaration

    Objective-C

    - (BOOL)contains:(nonnull BOOL (^)(T _Nonnull))block;

    Swift

    func contains(_ block: @escaping (Any) -> Bool) -> Bool