[ad_1]
I’m making an attempt to implement a tabView that takes a listing of things into pages that I might swipe backwards and forwards. Nevertheless, it retains bugging out with an “index out of certain” error. It is complicated to me as a result of I by no means set index in any respect, and I do not know easy methods to power an index both…
Beneath are my code. Apologize for any naive code, I’m new to SwiftUI. Any helps are appreciated, thanks!
import SwiftUI
//@Printed non-public var listing = QuestionList.self
struct QuestionList: Codable {
var listing:[QuestionItem]
}
class QuestionItem: Codable, Identifiable {
var id: Int
var textual content: String
var kind: Int
var reply: String
}
struct ContentView: View {
@State non-public var qlist = [QuestionItem]()
@State non-public var isShowForm = false
@State non-public var q1 = true
@State non-public var reply = ""
@State non-public var isOn = [Bool]()
@State non-public var selectedTab = 0
func showForm() {
isShowForm = !isShowForm
let url = URL(string: "http://127.0.0.1:3000/query")!
let process = URLSession.shared.dataTask(with: url) {
knowledge, response, error in
if let error = error {
print(error)
return
}
guard let httpResponse = response as? HTTPURLResponse,
(200...299).accommodates(httpResponse.statusCode) else {
print(response)
return
}
guard let knowledge = knowledge else {
return
}
do {
let listing = strive JSONDecoder().decode([QuestionItem].self, from:knowledge)
qlist = listing
print(qlist[1])
for i in 0..<qlist.depend {
isOn.append(qlist[i].kind == 0)
}
print(isOn)
// print(isOn)
print(kind(of: qlist[1]))
} catch {
print("error: ", error)
}
}
process.resume()
}
var physique: some View {
Textual content("Whats up, world!")
.padding()
Button("Open Kind") {
self.showForm()
}
if (isShowForm) {
TabView(choice: $selectedTab) {
ForEach(qlist.indices, id: .self) { index in
if qlist[index].kind == 0 {
HStack {
Textual content("(self.qlist[index].textual content)")
// Toggle("", isOn: $isOn[index])
Toggle("", isOn: $q1)
}
} else {
VStack {
Textual content("(self.qlist[index].textual content)")
// .lineLimit(2)
// .multilineTextAlignment(.main)
.fixedSize(horizontal: false, vertical: true)
// .body(width: 300)
TextField("Enter your reply right here:", textual content: $qlist[index].reply) {
}
}
}
}
}
.tabViewStyle(.web page(indexDisplayMode: .all the time))
.indexViewStyle(.web page(backgroundDisplayMode: .all the time))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
[ad_2]
