extension RangeReplaceableCollection where Element == RESPValue
  • Converts the collection of RESPValueConvertible elements and appends them to the end of the array.


    This method guarantees that only one storage expansion will happen to copy the elements.



    public mutating func append<ValueCollection>(convertingContentsOf elementsToCopy: ValueCollection)
        ValueCollection: Collection,
        ValueCollection.Element: RESPValueConvertible
  • Adds the elements of a collection to this array, delegating the details of how they are added to the given closure.

    When your closure will be doing more than a simple transform of the element value, such as when you’re adding both the key and value from a KeyValuePair, you should set the overestimatedCountBeingAdded to a value you do not expect to exceed in order to prevent multiple allocations from the increasing element count.

    For example:

    let pairs = [
        "MyID": 30,
        "YourID": 31
    var values: [RESPValue] = []
    values.add(contentsOf: pairs, overestimatedCountBeingAdded: pairs.count * 2) { (array, element) in
        // element is a (key, value) tuple

    However, if you just want to apply a transform, you can do that more similarly to a call to the reduce methods:

    let valuesToConvert = [...] // some collection of non-`RESPValueConvertible` elements, such as third-party types
    let values: [RESPValue] = []
    values.add(contentsOf: valuesToConvert) { (array, element) in
        // your transform and insert/append implementation

    If the elementsToCopy has no elements, the closure is never called.



    public mutating func add<ValueCollection: Collection>(
        contentsOf elementsToCopy: ValueCollection,
        overestimatedCountBeingAdded: Int? = nil,
        _ closure: (inout Self, ValueCollection.Element) -> Void



    The collection of elements that will be added to the array in the closure.


    The number of elements that will be added to the array. If no value is provided, the size of the collection being copied will be used.


    A closure left to define how the collection’s element should be added into the array.