Evelyn
Evelyn

Reputation: 186

how to multiply label data with an Int?

Im trying to be able to multiply the data in the label of price(1-3) by the counterValue to show the price for for each option that is selected

So far my code can multiply the counterValue by the factor of the selected optionBtn(1-3) when selected

What Im trying to do is multiply the counter value by the label data of the Price labels

All price labels are Floats I've tried using this code Int(Float(modifyItems.cart.price1)) to replace the factor variable in the 'if statements' but still no success

SideNote: The data that populates the labels in the ModifyVC is passed from another VC. A delegate passes the data into the ModifyVC and the data is retrieved from cloud Firestore (as seen in the viewDidLoad)

thank in advanced for any help that is given

class ModifyViewController: UIViewController {

    private var counterValue = 1
    var lastSelectedWeightButton = RoundButton()
    var modifyItems: Cart!

    @IBOutlet weak var price1: UILabel!
    @IBOutlet weak var price2: UILabel!
    @IBOutlet weak var price3: UILabel!
    @IBOutlet weak var weight1: UILabel!
    @IBOutlet weak var weight2: UILabel!
    @IBOutlet weak var weight3: UILabel!

    @IBOutlet weak var lblQty: UILabel!
    @IBOutlet weak var modifyTotal: UILabel!

    @IBOutlet weak var option1: RoundButton!
    @IBOutlet weak var option2: RoundButton!
    @IBOutlet weak var option3: RoundButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        let formatter = NumberFormatter()
        formatter.maximumFractionDigits = 2
        formatter.numberStyle = .decimal
        price1.text = "$\(formatter.string(for: modifyItems.cart.price1)!)"
        price2.text = "$\(formatter.string(for: modifyItems.cart.price2)!)"
        price3.text = "$\(formatter.string(for: modifyItems.cart.price3)!)"
        weight1.text = modifyItems.cart.weight1
        weight2.text = modifyItems.cart.weight2
        weight3.text = modifyItems.cart.weight3

    }

    @IBAction func minusQty(_ sender: AnyObject) {
         if(counterValue != 1){
            counterValue -= 1
         }
         self.lblQty.text = "\(counterValue)"
         var factor = 1
         if option1.isSelected {
            factor = 13
        } else if option2.isSelected {
            factor = 39
        } else if option3.isSelected {
            factor = 72
        }
        modifyTotal.text = "$\(factor * counterValue)"
        print("Decrease Quantity")
    }
}

class Cart {
    var cart: Items!

    init(cart: Items) {

        self.cart = cart
    }
}

Upvotes: 0

Views: 125

Answers (2)

code cry
code cry

Reputation: 71

You just store the label data to (Int or Float).

var total = 0
if modifyTotal.text != " " {
  total = Int(modifyTotal.text.replacingOccurrences(of: "$", with: ""))!
}
let finalTotalValue = total + (factor * CounterValue)
modifyTotal.text = "$\(finalTotalValue)"

Upvotes: 1

Shivam Gaur
Shivam Gaur

Reputation: 541

You can use NSExpression to evaluate your expression like

let nExpression = "1 * 5"
let expression = NSExpression(format: nExpression)
var result = expression.expressionValue(with: nil, context: nil) as? Int 

Upvotes: 0

Related Questions