# Initialising array of array

**URL:** <https://forums.swift.org/t/initialising-array-of-array/77584>\
**Category:** Using Swift\
**Tags:** swift\
**Created:** [February 1, 2025, 12:34am UTC](https://forums.swift.org/t/initialising-array-of-array/77584 "2025-02-01T00:34:42Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![johnny1](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@johnny1](https://forums.swift.org/u/johnny1)\
**Post date:** [February 1, 2025, 1:01am UTC](https://forums.swift.org/t/initialising-array-of-array/77584/4 "2025-02-01T01:01:00Z")

</div>

There's two problems here - one Swift and one SwiftUI.

Your SwiftUI problem is that your `@State`'s `initalValue` should be a constant:

> [@Default init internal needs a workaround. Enough is enough](https://forums.swift.org/t/default-init-internal-needs-a-workaround-enough-is-enough/40931/10):
>
> Any consistent behavior you see is an accident. @State's initial value must be independent of any instance of the view. Chances are good this will be enforced at compile time when the necessary property wrapper functionality exists to do so.

The Swift problem (the reason it won't compile) is this line:

```swift

_selectedSoilTypes[bore] = State(initialValue: ...)

```

Before you assign a value to the `_selectedSoilTypes ` array at an index, you need to initialize the `_selectedSoilTypes` array.

So you’d need a line like this to satisfy the compiler:

```swift

// This will compile but still has the SwiftUI problem

_selectedSoilTypes = State(initalValue: ...)

```

Only after you give the array a value can you assign to the indices. But again, even it you get it to compile with this method the UI will not work correctly - you shouldn't assign the `@State`'s `initialValue` in your `init`. I'm not sure exactly what you're trying to do here without more context so I'm not sure what to suggest instead.

---

_[View the full topic](https://forums.swift.org/t/initialising-array-of-array/77584)._
