【问题标题】:Can't retrieve Int from Firebase无法从 Firebase 检索 Int
【发布时间】:2021-07-30 12:37:32
【问题描述】:

我正在尝试从 Firestore 中检索 Int,但我不能说“调用 'append' 的结果未使用” 这是我的代码:

class pointsM: ObservableObject {
@Published var point = 0
init() {
    let db = Firestore.firestore()
    let user = Auth.auth().currentUser?.uid
    db.collection("Male").document(user!).getDocument { (snap, error) in
        if error != nil{
            print("Error")
        }else{
            let points = snap!.get("Coins") as? Int ?? 0
            self.$point.append(points)
        }
    }
}

}

【问题讨论】:

  • self.$point.append(points) 更改为self.point.append(points)
  • 它给了我一个错误。 "引用实例方法 'append' 需要包装器 'Published.Publisher'"
  • 对不起,我刚刚意识到 self.point 不是一个数组。只需这样做self.point = snap?.get("Coins") as? Int ?? 0
  • 追加函数调用让我失望
  • 为什么要反复强制解包变量?

标签: swift google-cloud-firestore


【解决方案1】:

你的问题是你的类型冲突,以及危险的强制展开。

class PointsM: ObservableObject {
    @Published var point = 0

    init() {
        if let user = Auth.auth().currentUser?.uid {
            Firestore.firestore().collection("Male").document(user).getDocument { (snap, error) in
                if let doc = snap,
                    let point = doc.get("Coins") as? Int {
                    self.point = point
                } else if let error = error {
                    print(error)
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 2017-07-22
    相关资源
    最近更新 更多