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