<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "macCatalyst: 27.0.0 -",
    "macOS: 27.0.0 -",
    "tvOS: 27.0.0 -",
    "visionOS: 27.0.0 -",
    "watchOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/UniqueArray/init(capacity:initializingWith:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s11UniqueArrayVsRi_zrlE8capacity16initializingWithAByxGSi_ys10OutputSpanVyxGzqd__YKXEtqd__YKcs5ErrorRd__lufc"
  },
  "title" : "init(capacity:initializingWith:)"
}
-->

# init(capacity:initializingWith:)

Creates a new array with the specified capacity, directly initializing
its storage using an output span.

```
init<E>(capacity: Int, initializingWith body: @_lifetime(0: copy 0) (inout OutputSpan<Element>) throws(E) -> Void) throws(E) where E : Error
```

## Parameters

`capacity`

The storage capacity of the new array.

`body`

A callback that gets called at most once to directly
populate newly reserved storage within the array. The function
is allowed to add fewer than `capacity` items. The array is
initialized with however many items the callback adds to the
output span before it returns (or before it throws an error).

---

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)