我有一段代码,想要将Firestore的数据放入一个选择器(picker)中。我之前已经实现了将Firestore数据显示在选择器中,但是我无法选择它以在“选定视图”中显示,因此我重写了代码,并出现了以下错误“Initializer 'init(_:)' requires that 'getSchoolData' conform to 'StringProtocol' "
请谅解这可能听起来像一个愚蠢的问题,我只是似乎无法解决它。我的代码副本如下所示。我已经尝试了几周,但还是束手无策,请对我友善一点,我是编程新手。
提前感谢,
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
}
schoolData
中每个schoolName
的name
吗? - George