I followed the accepted answer to this link
How make work a Picker with an ObservedObject in SwiftUI?
but I get the message "Generic struct 'Picker' requires that 'String' conform to 'View'" in the struct GameListPicker
import SwiftUI
struct GameListPicker: View {
@ObservedObject var gameListViewModel = GameListViewModel()
@State private var selectedGameList = ""
var body: some View {
Picker(selection: $selectedGameList, label: ""){
ForEach(gameListViewModel.gameList) { gameList in
Text(gameList.gameName)
}
}
.onAppear() {
self.gameListViewModel.fetchData()
}
}
}
The GameListViewModel
import Foundation
import Firebase
class GameListViewModel: ObservableObject{
@Published var gameList = [GameListModel]()
let db = Firestore.firestore()
func fetchData() {
db.collection("GameData").addSnapshotListener {(querySnapshot, error) in
guard let documents = querySnapshot?.documents else {
print("No documents")
return
}
self.gameList = documents.map { queryDocumentSnapshot -> GameListModel in
let data = queryDocumentSnapshot.data()
let gameName = data["GameName"] as? String ?? ""
return GameListModel(id: gameName, gameName: gameName)
}
}
}
}
and the gameListModel
import Foundation
struct GameListModel: Codable, Hashable,Identifiable {
var id: String
//var id: String = UUID().uuidString
var gameName: String
}
I can't determine the problem
You should provide a parameter that conforms to View
protocol for the argument label:
in Picker
.
Replace:
Picker(selection: $selectedGameList, label: "") {
With:
Picker(selection: $selectedGameList, label: Text("")) {
If you only need a text you can use:
Picker("Some text", selection: $selectedGameList) { ...
If you don't want to have any labels for your picker (as you tried to use ""
) you can use an EmptyView
:
Picker(selection: $selectedGameList, label: EmptyView()) { ...
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.