SwiftUI:how to show data from DB with picker

how can I show the user.name from db with swiftui picker?

i have already gotten the data from db and saved in Object User

here is my code :

UserList.swift

import Foundation
import Combine

class UserList: ObservableObject {
    @Published var users: [User] = []

    init() {
        load()
    }

    func load() {
        let url = URL(string: "http://localhost:3000/user")!
        URLSession.shared.dataTask(with: url) { data, response, error in
            DispatchQueue.main.async {
                self.users = try! JSONDecoder().decode([User].self, from: data!)
            }
        }.resume()
    }
}

//struct User: Decodable, Identifiable {
struct User: Decodable {
    var id: Int
    var name: String
}

ContentView.swift

import SwiftUI

struct ContentView: View {
   
    var body: some View {
        
        Picker(selection: /*@START_MENU_TOKEN@*/.constant(1)/*@END_MENU_TOKEN@*/, label: /*@START_MENU_TOKEN@*/Text("Picker")/*@END_MENU_TOKEN@*/) {
            /*@START_MENU_TOKEN@*/Text("1").tag(1)/*@END_MENU_TOKEN@*/
            /*@START_MENU_TOKEN@*/Text("2").tag(2)/*@END_MENU_TOKEN@*/
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

As I could not access your server, I mimicked the load() func.

Tested, I get John and Mary in Picker


But same logic will work with your load() from db.


import Foundation
import Combine
 
class UserList: ObservableObject {
    @Published var users: [User] = []
 
    init() {
        load()
    }
 
    func load() {
//        let url = URL(string: "http://localhost:3000/user")!
//        URLSession.shared.dataTask(with: url) { data, response, error in
//            DispatchQueue.main.async {
//                self.users = try! JSONDecoder().decode([User].self, from: data!)
//            }
//        }.resume()
        self.users = [User(id: 1, name: "John"), User(id: 2, name: "Mary")]
    }
}
 
//struct User: Decodable, Identifiable {
struct User: Decodable {
    var id: Int
    var name: String
}


import SwiftUI
 
struct ContentView: View {
    
    var body: some View {
        let users = UserList().users
//        return Picker(selection: /*@START_MENU_TOKEN@*/.constant(1)/*@END_MENU_TOKEN@*/, label: /*@START_MENU_TOKEN@*/Text("Picker")/*@END_MENU_TOKEN@*/) {
//            /*@START_MENU_TOKEN@*/Text("1").tag(1)/*@END_MENU_TOKEN@*/
//            /*@START_MENU_TOKEN@*/Text("2").tag(2)/*@END_MENU_TOKEN@*/
            return Picker(selection: /*@START_MENU_TOKEN@*/.constant(1)/*@END_MENU_TOKEN@*/, label: /*@START_MENU_TOKEN@*/Text("Picker")/*@END_MENU_TOKEN@*/) {
                Text(users[0].name).tag(1)
                Text(users[1].name).tag(2)
        }
    }
}
 
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

thank you for your answer,I have to show the data from my db,

I get the data shown on List,when I get the data shown on picker

I get the the error:Thread 1: Fatal error: Index out of range

it seems like i get nothing from users[]

do you know how to modify it?

I will really appreciate it if you can show me how to fill a foreach in picker to show all the data from users[]

here is my code:

ContentView.swift:

import SwiftUI
struct SwiftUIView_Main: View {
var body: some View {
    ContentViewpicker()
    //      ListView()
         }
}
struct ContentViewpicker: View {
     var body: some View {
        let users = UserList().users
       return Picker(selection: /*@START_MENU_TOKEN@*/.constant(1)/*@END_MENU_TOKEN@*/, label: /*@START_MENU_TOKEN@*/Text("Picker")/*@END_MENU_TOKEN@*/) {
                Text(users[0].name).tag(1)
        }
        }
}
struct ListView: View {
    @ObservedObject var store = UserList()
   var body: some View {
        List (store.users,id: \.id) { (user) in
            UserRow(user: user)
            }
}
}
struct UserRow: View {
    var user: User
    var body: some View {
        HStack {
            Text(String(user.id))
            Text(user.name)
        }
    }
}
struct SwiftUIView_Main_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView_Main().previewLayout(.fixed(width: 1194, height: 834))
    }
}

UserList.swift:

import Foundation
import Combine
class UserList: ObservableObject {
    @Published var users: [User] = []
init() {
        load()
    }
func load() {
        let url = URL(string: "http://localhost:3000/user")!
        URLSession.shared.dataTask(with: url) { data, response, error in
            DispatchQueue.main.async {
                self.users = try! JSONDecoder().decode([User].self, from: data!)
            }
        }.resume()
       }
}
//struct User: Decodable, Identifiable {
struct User: Decodable {
    var id: Int
    var name: String
}
SwiftUI:how to show data from DB with picker
 
 
Q