简体   繁体   中英

SwiftUI Picker population problem - ForEach Loop in picker does not populate

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
       }

   ]

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) ;}
                }
            }}}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM