Hi, this is a follow-up to “Where do I put this conditional?” I’ve put in a function, setupB, to put in 81 buttons where the labels are made up from an 81 character string that summarizes either the original sudoku puzzle or the solution. My question is this: how do I get that code into a view as 81 buttons? Thank you.
There’s no way to do that in Swift. Swift is a compiled language that emphasizes, among other things, type safety and being able to turn random strings into executable code goes against that principle.
Plus, that’s just not the way to do something like that in SwiftUI. Instead, you would use a ForEach to loop through some collection of data and generate elements for your interface.
ForEach(Array(yourBigString), id: \.self) { character in
//character is one character in the yourBigString
Button {
buttonWasTapped(currTag: String(character))
} label: {
Text(character)
.foregroundColor(...)
}
}
And you would use some logic outside the loop, like in a computed property or something, to determine if yourBigString would be the original or the solution.
Thanks, roosterboy, is there a way to enumerate character?
var n = 0
ForEach(Array(arrayLiteral: (showSolution ? solution : original)), id: \.self) { character in
//character is one character in the yourBigString
Button {
buttonWasTapped(currTag: n)
} label: {
Text(character)
.foregroundColor(character == "0" ? cDynamic : cStatic)
}
// n += 1 // can't do this in a view...
}.tag(n)
//enumerated() gives back an array of tuples (offset, element)
//offset will be an Int
//element will be a Character
ForEach(Array((showSolution ? solution : original).enumerated()), id: \.offset) { (offset, element) in
Button {
buttonWasTapped(currTag: offset)
} label: {
Text(String(element))
.foregroundColor(element == "0" ? cDynamic : cStatic)
}
.tag(offset)
}
That’s gross. I would seriously suggest pulling that into some kind of helper, whether a computed property or a function, to hide the logic from the ForEach and just give it a collection to be looped through. The more logic you get out of a View's body property, the happier SwiftUI and the compiler will be.
That’s fantastic!. Thank you roosterboy. I’m still unclear on how I could place those buttons in their correct positions.
I’ll attach a screenshot from the simulator that I completed last night, Here’s a code segment that goes up to the the first three characters of the string:
I’m sure that you’ll see this as even more “gross” than your snippet. That one liner is elegant. Thank you for your help. I’d be way behind without it.
Rather than hard-coding a bunch of HStacks, you would do something like what I posted here. It uses nested LazyVGrids to lay out all the numbers in their 9 9x9 grids.