【问题标题】:Initializer 'init(_:)' requires that '' conform to 'StringProtocol' SwiftUI Picker with FirebaseInitializer 'init(_:)' 要求 '' 符合 'StringProtocol' SwiftUI Picker with Firebase
【发布时间】:2021-02-10 05:39:45
【问题描述】:

我有一段代码试图将 Firestore 数据放在选择器中。我之前已经这样做了,以便选择器显示 Firestore 数据,但我无法选择它以显示在“选定视图”中,因此我重写了代码并出现以下错误 "Initializer 'init(_: )' 要求 'getSchoolData' 符合 '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 中每个schoolNamename?
  • 上周你似乎也有一个来自your previous question的工作版本。
  • 嗨,乔治,感谢您的回复,是的。我想在选择器中打印来自 Firebase 的名称,然后将其传递给选择器的主视图。我有这个工作,但无法选择任何东西,所以数据不会通过。

标签: swift firebase google-cloud-firestore swiftui


【解决方案1】:

您可以尝试以下方法:

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")
        }
    }
}

【讨论】:

  • 你绝对的传奇!我努力了这么久!它编译并运行不良现在看看我哪里出错了!再次感谢您!
猜你喜欢
  • 2020-08-10
  • 2021-11-24
  • 2020-01-23
  • 1970-01-01
  • 2022-07-20
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
  • 1970-01-01
相关资源
最近更新 更多