NSArray(FunkyUtilities)

@interface NSArray <__covariant ObjectType>
(FunkyUtilities) @end

This extension provides simple and easy to use functional and general utilities for NSArray. If you need to prefix the extension methods in this category, you should import NSArray+FunkyPrefixedUtilities.h, where every utility method is prefixed with the funky_ keyword for compatiblitiy reasons.

See

Prefixed counterpart NSArray(FunkyPrefixedUtilities)

See

Mutable counterpart NSMutableArray(FunkyUtilities)
  • Returns whether the condition matches all elements in the array

    Declaration

    Objective-C

    - (BOOL)all:(BOOL (^)(ObjectType))block;

    Swift

    func all(_ block: ((Any?) -> Bool)!) -> Bool

    Parameters

    block

    The condition given as a BOOL expression

    Return Value

    YES if condition matches all elements, NO otherwise

  • Returns whether the condition matches no elements in the array

    Declaration

    Objective-C

    - (BOOL)none:(BOOL (^)(ObjectType))block;

    Swift

    func none(_ block: ((Any?) -> Bool)!) -> Bool

    Parameters

    block

    The condition given as a BOOL expression

    Return Value

    NO if condition matches any of the elements, YES otherwise

  • Returns whether the condition matches at least one element in the array

    Declaration

    Objective-C

    - (BOOL)contains:(BOOL (^)(ObjectType))block;

    Swift

    func contains(_ block: ((Any?) -> Bool)!) -> Bool

    Parameters

    block

    The condition given as a BOOL expression

    Return Value

    YES if condition matches any of the elements, NO otherwise

  • Returns the number of elements the given condition matches in the array, like if you would filter the array.

    Declaration

    Objective-C

    - (NSUInteger)count:(BOOL (^)(ObjectType))block;

    Swift

    func count(_ block: ((Any?) -> Bool)!) -> UInt

    Parameters

    block

    The condition given as a BOOL expression

    Return Value

    Number of occasions where the condition matches in the array

  • Returns a new NSArray instance with the same amount of elements, where each element is transformed to another by returning a new object in the block parameter.

    Declaration

    Objective-C

    - (NSArray *)map:(id (^)(ObjectType))block;

    Swift

    func map(_ block: ((Any?) -> Any?)!) -> [Any]!

    Parameters

    block

    The transformator code which should return a new value based on the existing one.

    Return Value

    A new NSArray instance where each element is formed by the result of the block calls

  • Returns a new NSArray instance where each element is transformed to another by returning a new object in the block parameter. It ignores the nil parameters returned from the block.

    Declaration

    Objective-C

    - (NSArray *)nilTolerantMap:(id (^)(ObjectType))block;

    Swift

    func nilTolerantMap(_ block: ((Any?) -> Any?)!) -> [Any]!

    Parameters

    block

    The transformator code which should return a new value based on the existing one.

    Return Value

    A new NSArray instance where each element is formed by the result of the block calls

  • Same as map, but the block contains the index of the current element.

    Declaration

    Objective-C

    - (NSArray *)mapWithIndex:(id (^)(NSUInteger, ObjectType))block;

    Swift

    func map(index block: ((UInt, Any?) -> Any?)!) -> [Any]!

    Parameters

    block

    The transformator code which should return a new value based on the index and the existing item.

    Return Value

    A new NSArray instance where each element is formed by the result of the block calls

  • Same as nil-tolerant map, but the block contains the index of the current element.

    Declaration

    Objective-C

    - (NSArray *)nilTolerantMapWithIndex:(id (^)(NSUInteger, ObjectType))block;

    Swift

    func nilTolerantMap(index block: ((UInt, Any?) -> Any?)!) -> [Any]!

    Parameters

    block

    The transformator code which should return a new value based on the index and the existing item.

    Return Value

    A new NSArray instance where each element is formed by the result of the block calls

  • Returns a new NSArray instance with the same amount of elements, where each element is transformed to another by returning a new object in the block parameter. Same as map, but the result is going to be flattened, so if you return an NSArray any iteration, it is going to be converted into a flat structure, not an array of arrays.

    Declaration

    Objective-C

    - (NSArray *)flatMap:(id (^)(ObjectType))block;

    Swift

    func flatMap(_ block: ((Any?) -> Any?)!) -> [Any]!

    Parameters

    block

    The transformator code which should return a new value based on the existing one.

    Return Value

    A new NSArray instance where each element is formed by the result of the flattened block calls

  • Same as flatMap, but the block contains the index of the current element.

    Declaration

    Objective-C

    - (NSArray *)flatMapWithIndex:(id (^)(NSUInteger, ObjectType))block;

    Swift

    func flatMap(index block: ((UInt, Any?) -> Any?)!) -> [Any]!

    Parameters

    block

    The transformator code which should return a new value based on the index and the existing item.

    Return Value

    A new NSArray instance where each element is formed by the result of the flattened block calls

  • Returns a new NSArray instance with the element, that are passing the returned expression in the original array.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)filtered:(BOOL (^)(ObjectType))block;

    Swift

    func filtered(_ block: ((Any?) -> Bool)!) -> [Any]!

    Parameters

    block

    The filtering predicate given as a BOOL expression

    Return Value

    A new NSArray instance where each element is selected from the original one where the block returned YES

  • Flattens the array, meaning that if it consisted of Array items, they are going to be flattened into one flat structure of elements. An array of arrays will transform to an array of elements from each of the previous arrays. This computation is performed deeply, meaning that it contontued flattening elements, until they produce a flat structure.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)flattened;

    Swift

    func flattened() -> [Any]!

    Return Value

    A new NSArray instance where the elements don’t contain NSArrays

  • Returns a new NSArray instance, in which concatenates the two arrays by putting the existing elements first, and the elements in the provided array after them.

    Declaration

    Objective-C

    - (NSArray *)merged:(NSArray *)array;

    Swift

    func merged(_ array: [Any]!) -> [Any]!

    Parameters

    array

    The other collection to merge with

    Return Value

    A new NSArray which contains the elements of both arrays (self and the array parameter)

  • Groups elements to an NSDictionary, where the returned element serves as a key, and the objects as the value. If multiple elements are returned with the same key, this function will use the first matching element.

    Declaration

    Objective-C

    - (NSDictionary<id, ObjectType> *)groupByUsingFirst:(id (^)(ObjectType))block;

    Swift

    func groupBy(first block: ((Any?) -> Any?)!) -> [AnyHashable : Any]!

    Parameters

    block

    A block which returns a key (based on the passed element) used as the key in the dictionary for that element.

    Return Value

    An NSDictionary where keys are produced by the result of the block call, and the values are the original elements

  • Groups elements to an NSDictionary, where the returned element serves as a key, and the objects as the value. If multiple elements are returned with the same key, this function will use the last matching element.

    Declaration

    Objective-C

    - (NSDictionary<id, ObjectType> *)groupByUsingLast:(id (^)(ObjectType))block;

    Swift

    func groupBy(last block: ((Any?) -> Any?)!) -> [AnyHashable : Any]!

    Parameters

    block

    A block which returns a key (based on the passed element) used as the key in the dictionary for that element.

    Return Value

    An NSDictionary where keys are produced by the result of the block call, and the values are the original elements

  • Groups elements to an NSDictionary, where the returned element serves as a key, and the objects as the value. The elements in the resulting Dictionary are arrays, so if multiple elements return the same keys, all of them are going to be included in the value.

    Declaration

    Objective-C

    - (NSDictionary<id, NSArray<ObjectType> *> *)associateBy:
        (id (^)(ObjectType))block;

    Swift

    func associate(by block: ((Any?) -> Any?)!) -> [AnyHashable : [Any]]!

    Parameters

    block

    A block which returns a key (based on the passed element) used as the key in the dictionary.

    Return Value

    An NSDictionary where keys are produced by the result of the blocks, and the values are an array of original elements

  • Produces an aggregated value based on the elements of the array.

    Declaration

    Objective-C

    - (id)reduce:(id (^)(id, ObjectType))block withInitialValue:(id)start;

    Swift

    func reduce(_ block: ((Any?, Any?) -> Any?)!, withInitialValue start: Any!) -> Any!

    Parameters

    start

    The value to start with. At first this is going to be the rolling value.

    block

    The computation logic, which takes the rolling value and the current item and aggregates them using some custom logic.

    Return Value

    A custom value which is produced by computing all the elements with a custom logic, returned in block

  • A special use-case of reduce, which summarises the returned double values for each element in the array.

    Declaration

    Objective-C

    - (double)sum:(double (^)(ObjectType))block;

    Swift

    func sum(_ block: ((Any?) -> Double)!) -> Double

    Parameters

    block

    A block which returns double value, based on the current element.

    Return Value

    The sum of all elements, where the numbers are computed are transformed based on the original elements

  • A special use-case of reduce, which takes the average value of the returned double values for each element in the array.

    Declaration

    Objective-C

    - (double)average:(double (^)(ObjectType))block;

    Swift

    func average(_ block: ((Any?) -> Double)!) -> Double

    Parameters

    block

    A block which returns double value, based on the current element.

    Return Value

    The average of all elements, where the numbers are computed are transformed based on the original elements

  • A special use-case of reduce, which takes the minimum of the returned double values for each element in the array.

    Declaration

    Objective-C

    - (double)minValue:(double (^)(ObjectType))block;

    Swift

    func minValue(_ block: ((Any?) -> Double)!) -> Double

    Parameters

    block

    A block which returns double value, based on the current element.

    Return Value

    The minimum of all elements, where the numbers are computed are transformed based on the original elements

  • A special use-case of reduce, which takes the maximum of the returned double values for each element in the array.

    Declaration

    Objective-C

    - (double)maxValue:(double (^)(ObjectType))block;

    Swift

    func maxValue(_ block: ((Any?) -> Double)!) -> Double

    Parameters

    block

    A block which returns double value, based on the current element.

    Return Value

    The maximum of all elements, where the numbers are computed are transformed based on the original elements

  • Returns an array with all the minimal value elements in the array, where the minimum was computed by the returned double value for each element in the array.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)minItems:(double (^)(ObjectType))block;

    Swift

    func minItems(_ block: ((Any?) -> Double)!) -> [Any]!

    Parameters

    block

    A block which returns double value, based on the current element.

    Return Value

    The array of elements with the minimal value, where the numbers are computed are transformed based on the original elements

  • Returns an array with all the maximal value elements in the array, where the maximum was computed by the returned double value for each element in the array.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)maxItems:(double (^)(ObjectType))block;

    Swift

    func maxItems(_ block: ((Any?) -> Double)!) -> [Any]!

    Parameters

    block

    A block which returns double value, based on the current element.

    Return Value

    The array of elements with the maximal value, where the numbers are computed are transformed based on the original elements

  • Returns the first index of the array: 0

    Declaration

    Objective-C

    - (NSUInteger)firstIndex;

    Swift

    func firstIndex() -> UInt

    Return Value

    the first index of the array

  • Returns the last index of the array: count - 1

    Declaration

    Objective-C

    - (NSUInteger)lastIndex;

    Swift

    func lastIndex() -> UInt

    Return Value

    the last index of the array

  • Returns the first element, where the predicate matches

    Declaration

    Objective-C

    - (id)first:(BOOL (^)(ObjectType))block;

    Swift

    func first(_ block: ((Any?) -> Bool)!) -> Any!

    Parameters

    block

    The condition provided as a BOOL expression.

    Return Value

    The first element where the block returns YES

  • Returns the index of the first element, where the predicate matches

    Declaration

    Objective-C

    - (NSUInteger)firstIndex:(BOOL (^)(ObjectType))block;

    Swift

    func firstIndex(_ block: ((Any?) -> Bool)!) -> UInt

    Parameters

    block

    The condition provided as a BOOL expression.

    Return Value

    The index of the first element where the block returns YES

  • Returns the last element, where the predicate matches

    Declaration

    Objective-C

    - (id)last:(BOOL (^)(ObjectType))block;

    Swift

    func last(_ block: ((Any?) -> Bool)!) -> Any!

    Parameters

    block

    The condition provided as a BOOL expression.

    Return Value

    The index of the first element where the block returns YES

  • Returns the index of the last element, where the predicate matches

    Declaration

    Objective-C

    - (NSUInteger)lastIndex:(BOOL (^)(ObjectType))block;

    Swift

    func lastIndex(_ block: ((Any?) -> Bool)!) -> UInt

    Parameters

    block

    The condition provided as a BOOL expression.

    Return Value

    The index of the last element where the block returns YES

  • Returns an array of the values where the predicate matches, but only until it’s a consecutive sequence. Once the predicate returns NO, those elements are going to be dropped.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)take:(BOOL (^)(ObjectType))block;

    Swift

    func take(_ block: ((Any?) -> Bool)!) -> [Any]!

    Parameters

    block

    The condition provided as a BOOL expression.

    Return Value

    The first subsequence of elements until the block returns YES

  • Returns an array of the values where the predicate matches, but only the last consecutive sequence of these element. Same as take, but from the end of the array.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)takeLast:(BOOL (^)(ObjectType))block;

    Swift

    func takeLast(_ block: ((Any?) -> Bool)!) -> [Any]!

    Parameters

    block

    The condition provided as a BOOL expression.

    Return Value

    The last subsequence of elements from where the block returns YES

  • Returns an array of the values starting from the given object, not including the object itself. If the given object does not exist in the array, the result is going to be an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)fromValueExclusive:(id)value;

    Swift

    func fromValueExclusive(_ value: Any!) -> [Any]!

    Parameters

    value

    The signaling object.

    Return Value

    A subsequence of elements starting from the first occurence of the given object

  • Returns an array of the values starting from the given object, including the object itself. If the given object does not exist in the array, the result is going to be an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)fromValueInclusive:(id)value;

    Swift

    func fromValueInclusive(_ value: Any!) -> [Any]!

    Parameters

    value

    The signaling value, which is going to be represent in the array as the first element.

    Return Value

    A subsequence of elements starting from the first occurence of the given object

  • Returns an array of the values starting from the given index, not including the object at the given index. If the given index does not exist in the array, the result is going to be an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)fromIndexExclusive:(NSInteger)index;

    Swift

    func fromIndexExclusive(_ index: Int) -> [Any]!

    Parameters

    index

    The starting index.

    Return Value

    A subsequence of elements starting from the given index

  • Returns an array of the values starting from the given index, including the object at the fiven index. If the given index does not exist in the array, the result is going to be an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)fromIndexInclusive:(NSInteger)index;

    Swift

    func fromIndexInclusive(_ index: Int) -> [Any]!

    Parameters

    index

    The signaling index, where the element at the index is going to be represent in the array as the first element.

    Return Value

    A subsequence of elements starting from the given index

  • Returns an array of the values until the given object, not including the object itself. If the given object does not exist in the array, the result is going to be an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)untilValueExclusive:(id)value;

    Swift

    func untilValueExclusive(_ value: Any!) -> [Any]!

    Parameters

    value

    The signaling object.

    Return Value

    A subsequence of elements until the first occurence of the given object

  • Returns an array of the values until the given object, including the object itself. If the given object does not exist in the array, the result is going to be an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)untilValueInclusive:(id)value;

    Swift

    func untilValueInclusive(_ value: Any!) -> [Any]!

    Parameters

    value

    The signaling value, which is going to be represent in the array as the last element.

    Return Value

    A subsequence of elements until the first occurence of the given object

  • Returns an array of the values until the given index, not including the object at the given index. If the given index does not exist in the array, the result is going to be an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)untilIndexExclusive:(NSInteger)index;

    Swift

    func untilIndexExclusive(_ index: Int) -> [Any]!

    Parameters

    index

    The starting index.

    Return Value

    A subsequence of elements until the given index

  • Returns an array of the values until the given index, including the object at the fiven index. If the given index does not exist in the array, the result is going to be an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)untilIndexInclusive:(NSInteger)index;

    Swift

    func untilIndexInclusive(_ index: Int) -> [Any]!

    Parameters

    index

    The signaling index, where the element at the index is going to be represent in the array as the last element.

    Return Value

    A subsequence of elements until the given index

  • Returns an array of the values between the given two input objects, excluding both from the result set. If the starting object does not exist in the array, it’s equivalent to the matching untilValue expression. If the ending object does not exist in the array, it’s equivalent to the matching fromValue expression. If neither one exists, the result is an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)fromValueExclusive:(id)from
                            untilValueExclusive:(id)until;

    Swift

    func fromValueExclusive(_ from: Any!, untilValueExclusive until: Any!) -> [Any]!

    Parameters

    from

    The starting object.

    until

    The ending object.

    Return Value

    A subsequence of elements between the first occurences given objects

  • Returns an array of the values between the given two input objects, excluding from, including until from the result set. If the starting object does not exist in the array, it’s equivalent to the matching untilValue expression. If the ending object does not exist in the array, it’s equivalent to the matching fromValue expression. If neither one exists, the result is an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)fromValueExclusive:(id)from
                            untilValueInclusive:(id)until;

    Swift

    func fromValueExclusive(_ from: Any!, untilValueInclusive until: Any!) -> [Any]!

    Parameters

    from

    The starting object.

    until

    The ending object.

    Return Value

    A subsequence of elements between the first occurences given objects

  • Returns an array of the values between the given two input objects, including from, excluding until from the result set. If the starting object does not exist in the array, it’s equivalent to the matching untilValue expression. If the ending object does not exist in the array, it’s equivalent to the matching fromValue expression. If neither one exists, the result is an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)fromValueInclusive:(id)from
                            untilValueExclusive:(id)until;

    Swift

    func fromValueInclusive(_ from: Any!, untilValueExclusive until: Any!) -> [Any]!

    Parameters

    from

    The starting object.

    until

    The ending object.

    Return Value

    A subsequence of elements between the first occurences given objects

  • Returns an array of the values between the given two input objects, including both from the result set. If the starting object does not exist in the array, it’s equivalent to the matching untilValue expression. If the ending object does not exist in the array, it’s equivalent to the matching fromValue expression. If neither one exists, the result is an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)fromValueInclusive:(id)from
                            untilValueInclusive:(id)until;

    Swift

    func fromValueInclusive(_ from: Any!, untilValueInclusive until: Any!) -> [Any]!

    Parameters

    from

    The starting object.

    until

    The ending object.

    Return Value

    A subsequence of elements between the first occurences given objects

  • Returns an array of the values between the given two indices, excluding both objects (at the given indices) from the result set. If the starting index does not exist in the array, it’s equivalent to the matching untilIndex expression. If the ending index does not exist in the array, it’s equivalent to the matching fromIndex expression. If neither one exists, the result is an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)fromIndexExclusive:(NSInteger)from
                            untilIndexExclusive:(NSInteger)until;

    Swift

    func fromIndexExclusive(_ from: Int, untilIndexExclusive until: Int) -> [Any]!

    Parameters

    from

    The starting index.

    until

    The ending index.

    Return Value

    A subsequence of elements between the first occurences given indices

  • Returns an array of the values between the given two indices, excluding the object at the index from, including the one at the index until from the result set. If the starting index does not exist in the array, it’s equivalent to the matching untilIndex expression. If the ending index does not exist in the array, it’s equivalent to the matching fromIndex expression. If neither one exists, the result is an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)fromIndexExclusive:(NSInteger)from
                            untilIndexInclusive:(NSInteger)until;

    Swift

    func fromIndexExclusive(_ from: Int, untilIndexInclusive until: Int) -> [Any]!

    Parameters

    from

    The starting index.

    until

    The ending index.

    Return Value

    A subsequence of elements between the first occurences given indices

  • Returns an array of the values between the given two indices, including the object at the index from, excluding the one at the index until from the result set. If the starting index does not exist in the array, it’s equivalent to the matching untilIndex expression. If the ending index does not exist in the array, it’s equivalent to the matching fromIndex expression. If neither one exists, the result is an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)fromIndexInclusive:(NSInteger)from
                            untilIndexExclusive:(NSInteger)until;

    Swift

    func fromIndexInclusive(_ from: Int, untilIndexExclusive until: Int) -> [Any]!

    Parameters

    from

    The starting index.

    until

    The ending index.

    Return Value

    A subsequence of elements between the first occurences given indices

  • Returns an array of the values between the given two indices, including both objects (at the given indices) from the result set. If the starting index does not exist in the array, it’s equivalent to the matching untilIndex expression. If the ending index does not exist in the array, it’s equivalent to the matching fromIndex expression. If neither one exists, the result is an empty NSArray.

    Declaration

    Objective-C

    - (NSArray<ObjectType> *)fromIndexInclusive:(NSInteger)from
                            untilIndexInclusive:(NSInteger)until;

    Swift

    func fromIndexInclusive(_ from: Int, untilIndexInclusive until: Int) -> [Any]!

    Parameters

    from

    The starting index.

    until

    The ending index.

    Return Value

    A subsequence of elements between the first occurences given indices