Initialising array of array

Hi, VERY new to swift/swiftui and doing a favor for a friend ...

trying to initialize a member of an array of array of strings ... but get errors as follows:

Cannot assign value of type 'State<[String]>' to subscript of type '[String]'
Referencing subscript 'subscript(_:)' requires wrapped value of type '[[String]]'

any suggestions appreciated

code excerpts as follows:

....
 var  bore: Int // passed from calling view
...    
// Constants
private let soilTypes = ["FILL", "SILT", "CLAY", "SAND", "XW ROCK", "DW ROCK"]
...
// allocated array of soil types Vs depth, one for each bore 
@State private var selectedSoilTypes: [[String]]
....
// init stuff
init( bore: Int) {
// init the passed value and row count 
self.bore = bore
let rowCount = 30
//init the soiltype [bore] array to def values
_selectedSoilTypes[bore] = State(initialValue: Array(repeating: soilTypes[0], count: rowCount))

This works for a single array but not for array of arrays ???

State generally never is used on its own, The @State declaration is sufficient. You can assign to the variable afterwards that same way you would a non-@State variable.

I haven't tested this, but I think you'd just want:

selectedSoilTypes[bore] = Array(repeating: soilTypes[0], count: rowCount)

Thank you, im not sure I understand the "wrapper" concept of _ and $ and your suggestion gets rid of the wrapper error but not the first "subscript" error ...

thank you :slight_smile:

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

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

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


_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:


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

3 Likes

It's a little weird to wrap one's head around. Essentially, @State exists so SwiftUI can be told that an important variable which changes the View has changed.

1 Like

wow thank you for the response .. being new to swift its gping to take a while to digest what you said ....

I suppose what I am trying to do is to initialize a variable number of arrays of strings to default values ... the code below:

_selectedSoilTypes = State(initialValue: Array(repeating: soilTypes[0], count: rowCount))

works with a single array .. I wanted to extend it to cover multiple arrays with the index passed into the View ...

I will have to look at what you have said and do some more research ..

thank you

You have overcomplicated things by getting State involved. People are trying to help you with State, but it is not actually relevant to the question. Tackle the idea without State involved, first.

Thanks to everyone for assisting ..

I have posted a very different way of doing this above in a new topic so will close this one oif I can ???