---
title: "init(rawCapacity:initializingWith:)"
framework: foundation
role: symbol
role_heading: Initializer
path: "foundation/data/init(rawcapacity:initializingwith:)"
---

# init(rawCapacity:initializingWith:)

Creates a data instance with the specified capacity, and then calls the given closure with an output span covering the instance’s uninitialized memory.

## Declaration

```swift
init<E>(rawCapacity capacity: Int, initializingWith initializer: @_lifetime(span: copy span) (inout OutputRawSpan) throws(E) -> Void) throws(E) where E : Error
```

## Parameters

- `capacity`: The number of bytes to allocate space for in the new Data.
- `initializer`: A closure to initialize the allocated memory. Parameters: span: An OutputRawSpan covering uninitialized memory with space for the specified number of bytes.

## Discussion

Discussion Inside the closure, initialize elements by appending to the OutputRawSpan. The OutputRawSpan keeps track of the initialized memory, ensuring safety. Its count at the end of the closure will become the count of the newly-initialized instance of Data. note: While the resulting Data may have a capacity larger than the requested amount, the OutputRawSpan passed to the closure will cover exactly the number of bytes requested.
