用 Swift 中的 firestore 值减去 Int

Subtract Int with firestore value in Swift

我正在尝试在我的应用程序中制作优惠券系统。 当用户在文本字段中输入内容时,它应该查找与文本匹配的优惠券。 我已经设法检索到正确的文档,但我不知道如何从购物车的小计中减去文档字段值“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 的代码 class - 小计:

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