【问题标题】:SwiftUI Picker with Cloud Firestore带有 Cloud Firestore 的 SwiftUI 选择器
【发布时间】:2021-02-04 02:08:27
【问题描述】:

我想知道我是否能够在这个问题上获得一些帮助,我已经尝试了一段时间让事情正常运行,并且能够将 Firestore 数据传递到选择器视图中,但我无法在“选定”区域中选择要查看的数据。我已经添加了我的代码和我的 Firestore 设置。

提前致谢。

import SwiftUI
import Firebase

struct SchoolDetailsView: View {
    
    @ObservedObject var schoolData = getSchoolData()
    @State var selectedSchool: String!

    var body: some View {
        VStack {
            Form {
                Section {
                    Picker(selection: $selectedSchool, label: Text("School Name")) {
                        ForEach(self.schoolData.datas) {i in
                            Text(self.schoolData.datas.count != 0 ? i.name : "No Schools Available").tag(i.name)

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

Firestore Setup Image

【问题讨论】:

    标签: xcode firebase google-cloud-firestore swiftui


    【解决方案1】:

    要解决上述代码的问题,您可以将标记转换为与 selectedSchool 变量相同的类型。这应该允许它是可选的,并且也更安全,因为它使用选项并允许选择器最初设置为 nil。

    示例代码:

    struct SchoolDetailsView: View {
        
        @ObservedObject var schoolData = getSchoolData()
        @State var selectedSchool: String?
    
        var body: some View {
            NavigationView {
                VStack {
                    Form {
                        Section {
                            Picker(selection: $selectedSchool, label: Text("School Name")) {
                                ForEach(self.schoolData.datas.sorted(by: { $0.name < $1.name } )) {i in
                                    Text(self.schoolData.datas.count != 0 ? i.name : "No Schools Available").tag(i.name as String?)
                                }
                            }
                            Text("Selected School: \(selectedSchool ?? "No School Selected")")
                        }
                    }.navigationBarTitle("Select your school")
                }
            }
        }
    }
    

    作为上述示例的替代方案,您还可以将 selectedSchool 变量更改为 schoolName 类型并将标签转换为 schoolName,这也可以。这种方法唯一需要注意的是 schoolName 类型必须符合 Hashable 协议。

    示例替代代码:

    struct schoolName: Identifiable, Hashable {
        var id: String
        var name: String
    }
    
    struct SchoolDetailsView: View {
        
        @ObservedObject var schoolData = getSchoolData()
        @State var selectedSchool: schoolName?
    
        var body: some View {
            NavigationView {
                VStack {
                    Form {
                        Section {
                            Picker(selection: $selectedSchool, label: Text("School Name")) {
                                ForEach(self.schoolData.datas.sorted(by: { $0.name < $1.name } )) {i in
                                    Text(self.schoolData.datas.count != 0 ? i.name : "No Schools Available").tag(i as schoolName?)
                                }
                            }
                            Text("Selected School: \(selectedSchool?.name ?? "No School Selected")")
                        }
                    }.navigationBarTitle("Select your school")
                }
            }
        }
    }
    

    这些代码示例中的任何一个都应该产生如下的工作选择器:

    最后,作为使用 SwiftUI 的默认列表视图选择器样式的任何人的附注,它必须包含在视图层次结构中某处的 NavigationView 中。当我第一次开始使用它们时,这让我大吃一惊:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      相关资源
      最近更新 更多