# Arrays and Initialisation: Bug or "feature"? (and some interesting array trickery!)

**URL:** <https://forums.swift.org/t/arrays-and-initialisation-bug-or-feature-and-some-interesting-array-trickery/77644>\
**Category:** Using Swift\
**Tags:** array, initialisation\
**Created:** [February 4, 2025, 4:39am UTC](https://forums.swift.org/t/arrays-and-initialisation-bug-or-feature-and-some-interesting-array-trickery/77644 "2025-02-04T04:39:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![geotech](https://avatars.discourse-cdn.com/v4/letter/g/91b2a8/32.png) [@geotech](https://forums.swift.org/u/geotech)\
**Post date:** [February 4, 2025, 4:39am UTC](https://forums.swift.org/t/arrays-and-initialisation-bug-or-feature-and-some-interesting-array-trickery/77644/1 "2025-02-04T04:39:43Z")

</div>

So is this a bug or feature or I am not understanding the rules!

I create a variable: `@State public var thisSite: bores`

The error I get when I create a variable is: _Return from initializer without initializing all stored properties_

Note: the init() for the struct initialises EVERY MEMBER (see code below)

so to fix this I call it explicitly in the View init(): `thisSite = bores.init()`

Why do I need to call the init() this way?

Why not simply: `thisSite()` or `thisSite.init()`

* * *

basic code which is minimal to reduce reading (see if you can spot some array trickery :-)!!)

```swift
// a single row of data from the soil table
struct boreRow {
    var type: String
    init()
    {
        self.type = "FILL"
    }
}

// each bore
struct boreData {
    var boreA: [boreRow]
    init ()
    {
        boreA = Array(repeating: boreRow(), count: 30)
    }
}

// the array of bores
struct bores {
    var boresA: [boreData]
    init ()
    {
        boresA = Array(repeating: boreData(), count: 2)
    }
}

```

---

<div class="post-metadata">

**Author:** ![Diggory](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/diggory/32/4320_2.png) [@Diggory](https://forums.swift.org/u/Diggory)\
**Post date:** [February 4, 2025, 7:22am UTC](https://forums.swift.org/t/arrays-and-initialisation-bug-or-feature-and-some-interesting-array-trickery/77644/2 "2025-02-04T07:22:47Z")

</div>

Sorry, not an answer for your question, but FYI, it’s good form to capitalise the first letter when defining types. E.g.

```swift
// a single row of data from the soil table
struct BoreRow {
    var type: String
    init()
    {
        self.type = "FILL"
    }
}

```

---

<div class="post-metadata">

**Author:** ![Diggory](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/diggory/32/4320_2.png) [@Diggory](https://forums.swift.org/u/Diggory)\
**Post date:** [February 4, 2025, 7:36am UTC](https://forums.swift.org/t/arrays-and-initialisation-bug-or-feature-and-some-interesting-array-trickery/77644/3 "2025-02-04T07:36:34Z")

</div>

Your code appears to compile for me in Swift Playground on iPad.

Sometimes Xcode can show lingering errors after you have actually fixed the error.

```swift
import SwiftUI

// a single row of data from the soil table
 struct BoreRow {
  var type: String
  init()
  {
      self.type = "FILL"
  }
}

// each bore
struct BoreData {
  var boreA: [BoreRow]
  init ()
  {
      boreA = Array(repeating: BoreRow(), count: 30)
  }
}

// the array of bores
struct Bores {
  var boresA: [BoreData]
  init ()
  {
      boresA = Array(repeating: BoreData(), count: 2)
  }
}

struct ContentView: View {
  
  let bores = Bores()
  var body: some View {
      VStack {
          Image(systemName: "globe")
              .imageScale(.large)
              .foregroundColor(.accentColor)
          Text("Hello, world!")
      }
  }
}

```

---

<div class="post-metadata">

**Author:** ![geotech](https://avatars.discourse-cdn.com/v4/letter/g/91b2a8/32.png) [@geotech](https://forums.swift.org/u/geotech)\
**Post date:** [February 4, 2025, 8:07am UTC](https://forums.swift.org/t/arrays-and-initialisation-bug-or-feature-and-some-interesting-array-trickery/77644/5 "2025-02-04T08:07:31Z")

</div>

Yes agree, using old C++ syntax ☹

---

<div class="post-metadata">

**Author:** ![geotech](https://avatars.discourse-cdn.com/v4/letter/g/91b2a8/32.png) [@geotech](https://forums.swift.org/u/geotech)\
**Post date:** [February 4, 2025, 8:34am UTC](https://forums.swift.org/t/arrays-and-initialisation-bug-or-feature-and-some-interesting-array-trickery/77644/6 "2025-02-04T08:34:05Z")

</div>

But let is a constant and cant be changed so has to be "var"

> [@Diggory](#):
>
> ```swift
> struct ContentView: View {
>   
> let bores = Bores()
> var body: some View {
> VStack {
> bores.boresA[0].BoreA[0].type = "FILL" // fails with must be var error!
> Image(systemName: "globe")
> .imageScale(.large)
> .foregroundColor(.accentColor)
> Text("Hello, world!")
> }
> }
> }
> 
> ```

but if you declare it as: `var thisSite = bores()` generates a " required to conform to view ..." error when you try to access the components ...

The way I have detailed is the ONLY way I have found to get it to work (and it does work) but I have no idea how it is working or why ...

---

<div class="post-metadata">

**Author:** ![Diggory](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/diggory/32/4320_2.png) [@Diggory](https://forums.swift.org/u/Diggory)\
**Post date:** [February 4, 2025, 8:56am UTC](https://forums.swift.org/t/arrays-and-initialisation-bug-or-feature-and-some-interesting-array-trickery/77644/7 "2025-02-04T08:56:51Z")

</div>

```swift

struct ContentView: View {
    @State public var bores = Bores()
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
            Text("Bores count: \(bores.boresA.count)")
            Text("BoresA Rows count: \(bores.boresA.first?.boreA.count ?? 0) ")
        }
    }
}

```

Works for me:

```swift
Hello, world!
Bores count: 2
BoresA Rows count: 30

```

Sorry, I'm probably missing something...

---

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [February 4, 2025, 12:09pm UTC](https://forums.swift.org/t/arrays-and-initialisation-bug-or-feature-and-some-interesting-array-trickery/77644/8 "2025-02-04T12:09:21Z")

</div>

Actually, this question is more about `SwiftUI` than `Swift` the language. 🙂
