微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Swift 中用 firestore 值减去 Int

如何解决在 Swift 中用 firestore 值减去 Int

我正在尝试在我的应用中制作优惠券系统。 当用户在文本字段中输入时,它应该查找与文本匹配的优惠券。 我设法检索了正确的文档,但我不知道如何从购物车的小计中减去文档字段值“percentOff”。 这是一些代码

获取文档的代码

func textFieldDidEndEditing(_ textField: UITextField) {
        if textField == couponTxt {
            let collectionRef = db.collection("coupons")
            collectionRef.whereField("name",isEqualTo: couponTxt.text!).getDocuments { (snapshot,err) in
                if let err = err {
                    print("Error getting document: \(err)")
                } else {
                    for document in (snapshot?.documents)! {
                        if document == document {
                            let data = document.data()
                            let couponData = Coupon.init(data: data)
                          print(document.documentID)
                            }
                            
                        }
                    }
                }
            }
        }

这是我获取优惠券数据的模型:


 init(data: [String: Any]) {

        self.name = data["name"] as? String ?? ""
        self.id = data["id"] as? String ?? ""
        self.percentOff = data["percentOff"] as? Double ?? 0.0
        self.kronerOff = data["kronerOff"] as? Double ?? 0.0
        self.usageLeft = data["usageLeft"] as? Int ?? 0

    }

来自我的 PaymentCart 类的代码 - 小计:

 var subtotal: Int {
        var amount = 0
        for item in cartItem {
            let priceOere = Int(item.price * 100)
            amount += priceOere
        }
        return amount
    }

所以我的问题是 - 现在我可以得到正确的文件,我怎样才能从 percentOff 中得到值并减去我当前的小计。 谢谢!

解决方法

当您获得优惠券文档并创建 Coupon 对象时,如果您想稍后访问它,您必须将其放在某处。现在,除了初始化它之外,您不会对 couponData 做任何事情。 subtotal 是一个需要访问 couponData 的计算属性,所以 couponData 必须在这个计算属性的范围内,所以就让它发生。例如,如果 subtotal 在视图控制器中,那么只需将 couponData 设为同一个视图控制器中的实例属性。然后 subtotal 可能看起来像这样:

var coupon: Coupon?

var subtotal: Int {
    var amount = 0
    for item in cartItem {
        let priceOere = Int(item.price * 100)
        amount += priceOere
    }

    if let coupon = coupon {
        let discountRate = 1 - coupon.percentOff // assuming a 10% off coupon is 0.1 and not 10.0
        return amount * discountRate
    } else {
        return amount
    }
}

但是在你到达这里之前,你必须给 coupon 赋值:

func textFieldDidEndEditing(_ textField: UITextField) {
    if textField == couponTxt {
        let collectionRef = db.collection("coupons")
        collectionRef.whereField("name",isEqualTo: couponTxt.text!).getDocuments { (snapshot,err) in
            if let err = err {
                print("Error getting document: \(err)")
            } else {
                for document in (snapshot?.documents)! {
                    if document == document {
                        let data = document.data()
                        let couponData = Coupon(data: data)
                        self.coupon = couponData // assign it to the instance property
                        print(document.documentID)
                }
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。