0

I have SwiftUI and XCode 11.2.1 environment.

In the following code, the The ForEach loop displays title, the List is also displayed, it works :

struct Todo: Codable, Identifiable {
    let userId, id: Int
    let title: String
    let completed: Bool

    enum CodingKeys: CodingKey {
        case userId, id, title, completed
    }
}
// SOME STUFF HERE ...
struct ContentView: View {
// SOME STUFF HERE...
    var body: some View {

            NavigationView {
                VStack (spacing: 15){

                     Text("Number of items: \(todoData.todos.count)"

                    ForEach(self.todoData.todos) { str in Text(String(str.title));}  // works

                    List(self.todoData.todos) { todo in
                        Text(todo.title) // works
                    }

However, the following ForEach loop does not display Text at all, where I use the todoData.todos.count:

ForEach(0 ..< (todoData.todos.count)) {
                    Text(String(self.todoData.todos[$0].userId))
                    }

Coming to the Picker population, when I use the following:

Picker( selection: $selectedItem, label: Text("MyItems"), content:
                {
                    ForEach(self.todoData.todos) { str in Text(str.title);}
                }
                )

=-- Nothing is displayed except for the "MyItems" picker name and 2 empty lines of picker listing.

Any suggestion / help will be appreciated.

=-- The

data is as follows:
[
    {
           "userId": 1,
           "id": 1,
           "title": "delectus aut autem",
           "completed": false
       },
       {
           "userId": 1,
           "id": 2,
           "title": "quis ut nam facilis et officia qui",
           "completed": false
       },
       {
           "userId": 1,
           "id": 3,
           "title": "quis ut nam facilis et officia qui",
           "completed": false
       },
    {
           "userId": 2,
           "id": 4,
           "title": "quis ut nam facilis et officia qui",
           "completed": false
       }

   ]

1 Answer 1

0

You may try this, it works in my Xcode:

@State var selectedItem: String = ""

var body: some View {

        NavigationView {
            VStack (spacing: 15){

                 Text("Number of items: \(todos.count)")

                ForEach(self.todos) { str in Text(String(str.title));}  // works

                List(self.todos) { todo in
                    Text(todo.title) // works
                }

                ForEach(0 ..< (todos.count)) {
                Text(String(self.todos[$0].userId))
                    }

                Picker(selection: self.$selectedItem, label: Text("MyItems")) {
                    ForEach(self.todos) { str in Text(str.title).tag(str.title) ;}
                }
            }}}}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the clarification. Seems like I need to enclose the whole thing inside a FORM for it to work. That seems to have also solved my problem.

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.