2

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

1
  • To proceed by steps, could you replace Text(gameList.gameName) with Text("Some Text"). If that "works", then initialize String with var gameName: String = "" Commented Jul 19, 2020 at 15:51

2 Answers 2

5

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("")) {
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. your answer and pawello2222's answer worked
1

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()) { ...

1 Comment

Thanks. your answer and Frankestein's answer worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.