<!--
{
  "availability" : [
    "iOS: 12.2.0 -",
    "iPadOS: 12.2.0 -",
    "macCatalyst: 12.2.0 -",
    "macOS: 10.14.4 -",
    "tvOS: 12.2.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 5.2.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/Array/append(addingCapacity:initializingWith:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sa6append14addingCapacity16initializingWithySi_ys10OutputSpanVyxGzqd__YKXEtqd__YKs5ErrorRd__lF"
  },
  "title" : "append(addingCapacity:initializingWith:)"
}
-->

# append(addingCapacity:initializingWith:)

Grows the array to have enough capacity for the specified number of
elements, then calls the closure with an output span covering the array’s
uninitialized memory.

```
mutating func append<E>(addingCapacity uninitializedCount: Int, initializingWith initializer: @_lifetime(span: copy span) (inout OutputSpan<Element>) throws(E) -> Void) throws(E) where E : Error
```

## Parameters

`uninitializedCount`

The number of new elements the array should have
space for.

`initializer`

A closure that initializes new elements.

- Parameters:
  - span: An `OutputSpan` covering uninitialized memory with
    space for the specified number of additional elements.

## Discussion

Inside the closure, initialize elements by appending to `span`. It
ensures safety by keeping track of the initialization state of the memory
At the end of the closure, `span`’s `count` elements will have been
appended to the array.

If the closure throws an error, the items appended until that point
will remain in the array.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)