【问题标题】:Why does SwiftUI display an error when I attempt to retrieve database info from Firebase?当我尝试从 Firebase 检索数据库信息时,为什么 SwiftUI 会显示错误?
【发布时间】:2020-10-26 03:23:24
【问题描述】:

我最近一直在学习在 iOS 开发中使用 Firebase,并且一直遇到此错误。我目前在我的数据库的“用户”集合中有两个文档,它们代表两个用户。我正在使用这个函数来从我的“用户”集合中检索所有数据:

func retrieveAllInfo() {
        db.collection("users").getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            }
            else {
                for document in querySnapshot!.documents {
                    print("\(document.documentID) => \(document.data())")
                }
            }
        }
    }

但是,当我尝试将此功能实现为 SwiftUI 按钮的操作时,我收到此错误:

无法将类型“()”的值转换为预期的参数类型“() -> Void”

我真的不明白发生了什么。我能够使用我编写的函数将用户写入数据库作为 SwiftUI 按钮的操作,但这特别会引发错误。在我看来,它似乎没有任何返回类型(或者至少与我为写入数据库而编写的函数不同)。这是我用来写入数据库的函数之一的示例:

    func addAlanTurning() { //Adding to a new document + collection
            var ref: DocumentReference? = nil
            ref = db.collection("users").addDocument(data: [
                "first": "Alan",
                "Middle": "Mathison",
                "last" : "Turing",
                "born" : 1912
            ]) { err in
                if let err = err {
                    print("error adding document: \(err)")
                } else {
                    print("Document added with ID: \(ref!.documentID)")
                }
            }
        }

任何帮助将不胜感激。提前谢谢你。

编辑:

我设法将操作添加到按钮,但是问题仍然以不同的形式存在。当我编写一个新函数来为每个用户的属性添加参数的用户时,当我尝试在填充参数的情况下向它添加函数时,该按钮会引发相同的错误。这是新功能:

func addUser(first: String, middle: String, last: String, age: Int) { //Adding to a new document + collection
        var ref: DocumentReference? = nil
        ref = db.collection("users").addDocument(data: [
            "first": first,
            "Middle": middle,
            "last" : last,
            "age" : age
        ]) { err in
            if let err = err {
                print("error adding document: \(err)")
            } else {
                print("Document added with ID: \(ref!.documentID)")
            }
        }
    }

这是它在引发错误的按钮中的实现方式:

struct ContentView: View {
    let db = Firestore.firestore()
    
    @State private var first: String = ""
    @State private var middle: String = ""
    @State private var last: String = ""
    @State private var age: Int = 25
    
    var body: some View {
        VStack {
            TextField("First Name:", text: $first)
            TextField("Middle Name:", text: $middle)
            TextField("Last Name:", text: $last)
            Stepper(value: $age, in: 13...99) {
                Text("Age: \(age)")
            }
            Button(action: addUser(first: first, middle: middle, last: last, age: age)) {
                Text("Add User")
            }
        }
        .padding()
    }

提前谢谢你。

【问题讨论】:

    标签: ios swift firebase google-cloud-firestore swiftui


    【解决方案1】:

    您必须在闭包周围放置花括号,如下所示:

    Button(action: { addUser(first: first, middle: middle, last: last, age: age) }, label: {
      Text("Button")
    })
    

    另外,我想建议您将所有数据访问代码移动到视图模型,甚至是存储/存储库中。以下文章很好地概述了如何做到这一点:

    【讨论】:

      猜你喜欢
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      相关资源
      最近更新 更多