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

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 :-)!!)

// 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)
    }
}


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

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

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.

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!")
      }
  }
}

Yes agree, using old C++ syntax :frowning:

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

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 ...


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:

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

Sorry, I'm probably missing something...

Actually, this question is more about SwiftUI than Swift the language. :slight_smile: