Hello,
I have problem with generating data from my array.
Here is example code.
import SwiftUI
import Foundation
struct Something: Identifiable {
var id=UUID()
var health: Double
var age: Int
var name: String
}
struct ContentView: View {
@State var something:[Something] = [
Something( health: 100, age: 10, name: "test 1"),
Something( health: 100, age: 10, name: "test 2"),
Something( health: 100, age: 10, name: "test 3"),
Something( health: 100, age: 10, name: "test 4"),
Something( health: 100, age: 10, name: "test 5"),
Something( health: 100, age: 10, name: "test 6"),
Something( health: 100, age: 10, name: "test 7"),
Something( health: 100, age: 10, name: "test 8"),
Something( health: 100, age: 10, name: "test 9"),
Something( health: 100, age: 10, name: "test 10"),
Something( health: 100, age: 10, name: "test 11"),
Something( health: 100, age: 10, name: "test 12")
]
private var columns: [GridItem] = [
GridItem(.fixed(100), spacing: 16),
GridItem(.fixed(100), spacing: 16)
]
var body: some View {
ScrollView{
LazyVGrid(columns: columns, spacing: 10,pinnedViews: [.sectionHeaders]){
ForEach(0 ..< 56) { pole in
Section(header: Text("Section \(pole)").font(.title)){
ForEach(0 ..< something.count, id:\.self) { index in
Text("test nr \(index)")
}
}
}
}
}
}
}
After run this code, I have that problem:
When I replace line code
from: ForEach(0 …< something.count, id:.self) { index in
to: ForEach(0 …< something.count) { index in
almost everything is ok but I got error
Non-constant range: argument must be an integer literal
but sections and data works ok
What is wrong in my example?
Piotr