I have following structure
struct ContentView: View {
@State private var usedWord = [String]()
@State private var rootWord = ""
@State private var newWord = ""
var manager = HttpRequest()
var body: some View {
NavigationView{
VStack{
TextField("Enter your symptom", text: $newWord, onCommit: addNewWord)
.textFieldStyle(RoundedBorderTextFieldStyle())
.autocapitalization(.none )
.padding()
List {
ForEach(usedWord, id: \.self){
Text($0)
}
.onDelete(perform: deleteItem)
}
Button("Get diagnose"){
// here we plac logic of sending request to API server
}
}
.navigationBarTitle(rootWord)
}
}
func addNewWord() {
let answer = newWord.lowercased( ).trimmingCharacters(in: .whitespacesAndNewlines)
guard answer.count > 0 else {
return
}
// extra validation to come
usedWord.insert(answer, at: 0)
newWord = ""
}
func deleteItem(at indexSet: IndexSet) {
self.usedWord.remove(atOffsets: indexSet)
}
}
It's a list of text items in it. In Button("Get diagnose") i would like to iterate over List and create Json object to sent it to API server. Json structure looks something like {'1': 'pain in chest', '2': 'headache'}. I have a request function, but i don't know how to create Json
usedWord, because you use your data source and not some UI components for that.let json = try JSONEncoder().encode(usedWord)