Hello
How do I solve the Index out of range at line 81
I tried many ways, but none of them worked
Thank you very much
How do I solve the Index out of range at line 81
I tried many ways, but none of them worked
Code Block // // ContentView.swift // Fede // // Created by Jad Taljabini on 14/03/21. // import SwiftUI struct ContentView: View { @State private var media: String = "" @State private var voto: String = "" @State private var voti: [Float] = [] @State private var nVoti: Int = 0 @State private var test: String = "" @State private var voti2: [Float] = [] let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() var body: some View { NavigationView { Form { Section{ TextField("A che media vuoi arrivare", text: $media) } Section{ TextField("Quanti voti prenderai", text: $test) } Section{ HStack { TextField("Tuoi voti", text: $voto) Spacer() Button(action: { voti.append(Float(voto) ?? 0) voto = "" nVoti = nVoti + 1 }, label: { Text("Add") }) } } Section{ ForEach(voti, id: \.self){ item in Text("\(item, specifier: "%.1f")") }.onDelete(perform: removeRows) } Section(header: Text("Voti che devi prendere")){ ForEach(voti2, id: \.self){ item2 in Text("\(item2)") } } }.onReceive(timer) { input in voti2 = tutto() } .navigationTitle("Media") } } func tutto() -> [Float] { let testInt = Int(test) ?? 0 let mediaFloat = Float(media) ?? 0 var mediaEffettiva = mediaEffFunzione(dim: nVoti) if mediaEffettiva < mediaFloat { for i in 0..<testInt - 2 { voti2[i] = Float(Int(mediaEffettiva + 1)) } let mediaEffettivaInt = Int(mediaEffettiva) let mediaFloatInt = Int(mediaFloat) var con: Int = 0 while(mediaEffettivaInt < mediaFloatInt){ for j in nVoti..<nVoti + testInt - 2{ voti[j] = voti2[j - nVoti]; } mediaEffettiva = mediaEffFunzione(dim: nVoti + testInt) if(mediaEffettiva < mediaFloat){ voti2[con] += 0.5 } con = con + 1 if con >= testInt { con = 0 } } } return voti2 } func removeRows(at offsets: IndexSet) { voti.remove(atOffsets: offsets) } func mediaEffFunzione(dim: Int) -> Float { var somma: Float = 0 for i in voti{ somma = somma + i } return somma / Float(dim) } }
Thank you very much
In my previous answer I told you:
Did you check how voti2 is initialised ?
BTW: that means that at start, mediaEffettivaInt < mediaFloatInt is true.
In addition, looks like this code is flawed:
mediaEffettivaInt and mediaFloatInt do not change in the loop: so either you never enter the loop or you loop indefinitely.
You do need to analyse and correct your code.
Note: you should select names that are more meaningful:
You did not answer to this.But the real point is to check if voti2.count is ever more than zero.
If not, that confirms there is a design flaw. This is what must be corrected.
Did you check how voti2 is initialised ?
Normal. Before the change, you crashed at first pass in the loop, before infinite loop ! You don't crash anymore, but you enter an infinite while loop.I changed it to this, but when I press Add, the simulator freezes
BTW: that means that at start, mediaEffettivaInt < mediaFloatInt is true.
In addition, looks like this code is flawed:
Code Block while(mediaEffettivaInt < mediaFloatInt){ for j in nVoti..<nVoti + testInt - 2{ voti[j] = voti2[j - nVoti]; } mediaEffettiva = mediaEffFunzione(dim: nVoti + testInt) if mediaEffettiva < mediaFloat && voti2.isValidIndex(con) { voti2[con] += 0.5 } con = con + 1 if con >= testInt { con = 0 } }
mediaEffettivaInt and mediaFloatInt do not change in the loop: so either you never enter the loop or you loop indefinitely.
You do need to analyse and correct your code.
Note: you should select names that are more meaningful:
mediaEffettivaInt and mediaEffettiva : difficult to understand at first glance what is what (both are Int)