mercredi 26 mai 2021

Unable to check two if conditions for two different values in swift

I want to check two conditions for two different values

here var stock_qty: Int?

  private func updateItemQuantity(isIncreasing: Bool) {
    
    productCount = isIncreasing ? productCount + 1 : productCount - 1
    minusBtn.isEnabled = productCount > 1
    print("stock: \(detailsDB?.result?.product?.product_details?.stock)")
    if let data = detailsDB?.result?.product?.product_details{
        
       for stockData in data.product_variants ?? []{
        print("stock_qty11111: \(stockData.stock_quantity)")
        stock_qty = stockData.stock_quantity
        
    
       }
    }
    print("outtttt stock \(stock_qty)")// here i am getting `stock_qty` = 5

    if productCount > stock_qty ?? 0{// taking 0, why?
       print("product not available")
       minusBtn.isEnabled = productCount > 1

       self.view.makeToast("Currently \(String(describing: detailsDB?.result?.product?.product_details?.stock ?? 0)) Products are available.",position : ToastPosition.center)
   }
    
     if productCount > (detailsDB?.result?.product?.product_details?.stock)!{
        print("product not available")
        minusBtn.isEnabled = productCount > 1

        self.view.makeToast("Currently \(String(describing: detailsDB?.result?.product?.product_details?.stock ?? 0)) Products are available.",position : ToastPosition.center)
    }
    else{
      
        self.lblProductCount.text = String(productCount)
        minusBtn.isEnabled = productCount > 1

    let detailsData = detailsDB?.result?.product?.product_details
    price = detailsData?.price ?? ""
    discountPrice = detailsData?.discount_price ?? ""
    
    
    self.lblDiscountPrice.text = discountPrice != "0.000" && compareDate(fromDate: detailsData?.from_date ?? "", toDate: detailsData?.to_date ?? "") ? "\((discountPrice as NSString).floatValue * Float(productCount)) KWD" : "\((price as NSString).floatValue * Float(productCount)) KWD"
    self.lblRealPrice.text = discountPrice != "0.000" && compareDate(fromDate: detailsData?.from_date ?? "", toDate: detailsData?.to_date ?? "") ? "\((price as NSString).floatValue * Float(productCount)) KWD" : ""
    self.lblLine.isHidden = discountPrice != "0.000" && compareDate(fromDate: detailsData?.from_date ?? "", toDate: detailsData?.to_date ?? "") ? false : true

    let formatter = NumberFormatter()
    formatter.locale = Locale(identifier: "en_US")
    let totalPrice = formatter.number(from: self.price)
    let discountedPrice = formatter.number(from: self.discountPrice)
    let savedPrice = (totalPrice?.floatValue ?? 0.0) - (discountedPrice?.floatValue ?? 0.0)
    let finalSavedPrice = String(format: "%.3f", (savedPrice))
    
    self.lblOffPrice.text = self.discountPrice != "0.000" && compareDate(fromDate: detailsData?.from_date ?? "", toDate: detailsData?.to_date ?? "") ? "\((finalSavedPrice as NSString).floatValue * Float(productCount)) KWD  (\(getOffPercrent(realPrice: self.price, discountedPrice: self.discountPrice) ?? ""))" : ""
    }
          
}

if productCount > stock_qty ?? 0 and productCount > (detailsDB?.result?.product?.product_details?.stock)! then only need to show tost message other wise else part to work.. how to do that?

i need to check two if conditions for one else, please do help me here

Aucun commentaire:

Enregistrer un commentaire