简体   繁体   中英

Initializer 'init(_:)' requires that '' conform to 'StringProtocol' SwiftUI Picker with Firebase

I have a piece of code where I'm trying to place Firestore data within a picker. I have made it previously so that the picker will show the Firestore data, but I am unable to select it to show in the 'selected view' therefore I rewrote the code and have the following error "Initializer 'init(_:)' requires that 'getSchoolData' conform to 'StringProtocol' "

Please excuse that it may sound like a daft question I just can't seem to solve it. A copy of my code is below. I have tried working on this for weeks but at a loss so please be kind, I'm new to coding.

Thanks in advance,

import SwiftUI
import Firebase

struct SchoolDetailsView: View {
    
    let schoolData = [getSchoolData()]
    @State var selectedSchool = 0

    var body: some View {
        VStack {
            Form {
                Section {
                    Picker(selection: $selectedSchool, label: Text("School Name")) {
                        ForEach(0 ..< schoolData.count) {
                            Text(self.schoolData[$0])

                        }
                    }
                    Text("Selected School: \(selectedSchool)")
                }
            }.navigationBarTitle("Select your school")

        }
    }
}

struct SchoolPicker_Previews: PreviewProvider {
    static var previews: some View {
        SchoolDetailsView()
    }
}

class getSchoolData : ObservableObject{
    
    @Published var datas = [schoolName]()
    
    init() {
        
        let db = Firestore.firestore()
        
        db.collection("School Name").addSnapshotListener { (snap, err) in
            
            if err != nil{
                
                print((err?.localizedDescription)!)
                return
            }
            
            for i in snap!.documentChanges{
                
                let id = i.document.documentID
                let name = i.document.get("Name") as! String
                
                self.datas.append(schoolName(id: id, name: name))
            }
        }
    }
}

struct schoolName : Identifiable {
    
    var id : String
    var name : String
}

You may try the following:

struct SchoolDetailsView: View {
    @ObservedObject var schoolData = getSchoolData() // make `@ObservedObject`/`@StateObject` instead of const array
    @State var selectedSchool = "" // `schoolName.id` is of type String

    var body: some View {
        VStack {
            Form {
                Section {
                    Picker(selection: $selectedSchool, label: Text("School Name")) {
                        ForEach(schoolData.datas, id: \.id) { // choose whether you want to tag by `id` or by `name`
                            Text($0.name)
                        }
                    }
                    Text("Selected School: \(selectedSchool)")
                }
            }.navigationBarTitle("Select your school")
        }
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM