0

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

1
  • 1
    The question is incorrect, you should ask how to create json from a string array, usedWord, because you use your data source and not some UI components for that. let json = try JSONEncoder().encode(usedWord) Commented May 5, 2020 at 6:36

1 Answer 1

1

You don't need to iterate over the list. You should iterate over the list's data like:

Button("Get diagnose"){
    // here we plac logic of sending request to API server
    for word in self.usedWord.enumerated() {
       print(word.offset, ":", word.element)
    }
}

I don't know how you need the JSON but, You can build a dictionary from that like:

let dictionary = Dictionary(uniqueKeysWithValues: zip(self.usedWord.indices, self.usedWord))

And the JSONData like:

let jsonData = try? JSONSerialization.data(withJSONObject: dictionary, options: [])
Sign up to request clarification or add additional context in comments.

2 Comments

This is perfect! But can self.usedWord.indices is integer and i get 'Invalid (non-string) key in JSON dictionary'. How can i convert self.usedWord.indices to String? I tried String(self.usedWord.indices) but it doesn't work
i figure out let converted = dictionary.map{[String($0) : String($1)]} Thank you for your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.