Search code examples
listswiftuisubview

Unsafe Pointer?


I am receiving this error message.

I am trying to make a subview that I can reuses to display a row in my app.

For some reason, it is pointing to the index variable that I am using to iterate over my enum in my List to display my data.

Why is this happening? I am not sure how to refer to this variable outside of the subview.

 struct DisplayRow: View {
    
    var name: String
    var code: String
    var value: Double
    var counter: Int
    
    var body: some View {
        
        List(0..<counter, id: \.self) {index in
            
            VStack {
                HStack {
                    Text(name)

                    Text(code)
                }
                Text("$\(value)")
            }
        }
        
    }
}

struct ContentView: View {
    
    var body: some View {
        List {
            Section(header: Text("Wounds")) {
                DisplayWoundRow()
            }
            Section(header: Text("Debride")) {
                DisplayRow(name: debride.allCases[index].rawValue, code: debridecode.allCases[index].rawValue, value: debridevalue.allCases[index].rawValue, counter: debride.allCases.count)
            }
        }

screenshot


Solution

  • You need ForEach for that, like

    Section(header: Text("Debride")) {
      ForEach(0..<debride.allCases.count, id: \.self) { index in          // << here !!
        DisplayRow(name: debride.allCases[index].rawValue, code: debridecode.allCases[index].rawValue, value: debridevalue.allCases[index].rawValue, counter: debride.allCases.count)
      }
    }
    

    *I don't have all those types and cannot test, so typos or other errors are on you.