Array

extension Array: YMSafeSubscriptable

Conditional Append, Insertion, and Removal

  • Adds a new element at the end of the array if the specified closure evaluates to true.

    Declaration

    Swift

    @inlinable
    public mutating func append(
        _ newElement: Element,
        if conditionClosure: @autoclosure () -> Bool
    )

    Parameters

    newElement

    The element to append to the array.

    conditionClosure

    The closure to evaluate.

  • Adds the elements of a sequence to the end of the array if the specified closure evaluates to true.

    Declaration

    Swift

    @inlinable
    public mutating func append<S>(
        contentsOf newElements: S,
        if conditionClosure: @autoclosure () -> Bool
    ) where Element == S.Element, S: Sequence

    Parameters

    newElements

    The elements to append to the array.

    conditionClosure

    The closure to evaluate.

  • Inserts a new element at the specified position if the specified closure evaluates to true.

    Declaration

    Swift

    @inlinable
    public mutating func insert(
        _ newElement: Element,
        at i: Int,
        if conditionClosure: @autoclosure () -> Bool
    )

    Parameters

    newElement

    The new element to insert into the array.

    i

    The position at which to insert the new element. index must be a valid index of the array or equal to its endIndex property.

    conditionClosure

    The closure to evaluate.

  • Removes and returns the element at the specified position if the specified closure evaluates to true.

    Declaration

    Swift

    @inlinable
    public mutating func remove(
        at index: Int,
        if conditionClosure: @autoclosure () -> Bool
    ) -> Element?

    Parameters

    index

    The position of the element to remove. index must be a valid index of the array.

    conditionClosure

    The closure to evaluate.