Swiftui:我可以在列表中显示db中的数据,但在选择器中什么也没有显示,我该如何解决 这是我的ContentView:
import SwiftUI
struct ContentView: View {
@ObservedObject var model = PostListViewModel()
@State private var selectedStrength = 0
var body: some View {
// List(model.posts) { post in
// Text(post.name)
// }
Picker(selection: $selectedStrength, label: Text("picker")) {
ForEach(model.posts) { post in
Text(post.name)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
答案 0 :(得分:0)
尝试此操作或类似操作:
ForEach(model.posts, id: \.self) { post in
Text(post.name)
}