---
title: "insert(addingCount:at:initializingWith:)"
framework: Swift
role: symbol
role_heading: Instance Method
platforms: [iOS 27.0+, iPadOS 27.0+, Mac Catalyst 27.0+, macOS 27.0+, tvOS 27.0+, visionOS 27.0+, watchOS 27.0+]
path: "swift/uniquearray/insert(addingcount:at:initializingwith:)"
---

# insert(addingCount:at:initializingWith:)

Inserts a given number of new items into this array at the specified position, using a callback to directly initialize array storage by populating an output span.

## Declaration

```swift
mutating func insert<E>(addingCount newItemCount: Int, at index: Int, initializingWith initializer: @_lifetime(0: copy 0) (inout OutputSpan<Element>) throws(E) -> Void) throws(E) where E : Error
```

## Parameters

- `index`: The position at which to insert the new items. index must be a valid index in the array.

## Discussion

Discussion Existing elements in the array’s storage are moved towards the back as needed to make room for the new items. If the array does not have sufficient capacity to hold the new elements, then this operation reallocates storage to extend its capacity, using a geometric growth rate. var buffer = UniqueArray<Int>() buffer.append([-999, 999]) var i = 0 buffer.insert(capacity: 3, at: 1) { target in   while !target.isFull {     target.append(i)     i += 1   } } // `buffer` now contains [-999, 0, 1, 2, 999] note: O(self.count + count)
