简体   繁体   中英

How to convert data passed by delegate to different values?

What i am trying to do is multiply and add the data passed from the delegate to get the sales tax and the total in the labels salesTaxLbl and totalLbl of the AdditionalCostsController

The func in the AdditionalCostsDelegate that passes data from the cartTotalLbl(TotalFooter) to the subtotalLbl(AdditionalCostsController) has no issues passing data to the label

I keep getting an error when I try to multiply inside the value taxes and when I try to add inside the value total

as soon as I press the feesBtn(additionalFeesOnClicked) I get an error code on either taxes or total in the AdditionalCostsDelegate with the error saying:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

my Delegate:

protocol AdditionalCostsDelegate: class {
    func onTouchAdditionalCostsInfo(info: String)
}

code palced in my TotalFooter cell to pass the delegate

weak var additionalCostsDelegate: AdditionalCostsDelegate? = nil

@IBAction func additionalFeesOnClicked(_ sender: AnyObject) {
    self.additionalCostsDelegate?.onTouchAdditionalCostsInfo(info: cartTotalLbl.text!)
}

code placed in CartViewController to handle delegate

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let totalFooter = tableView.dequeueReusableCell(withIdentifier: "TotalFooter") as! TotalFooter

   //...... ->

   <-  ...... // code used to get overallTotal

    totalFooter.cartTotalLbl.text = String(overallTotal!)
    totalFooter.additionalCostsDelegate = self

    return totalFooter
}

Delegate used to pass data to AdditionalCostsController

extension CartViewController: AdditionalCostsDelegate {
    func onTouchAdditionalCostsInfo(info: String) {
        let popUp = self.storyboard?.instantiateViewController(withIdentifier: "AdditionalCostsVC") as! AdditionalCostsController

        self.present(popUp, animated: true) {

            let taxes = Float(info)! * Float(0.0825) //where im getting my error ***
            let total = Float(info)! + Float(taxes)  //where im getting my error ***

            // convert to currency
            let numberFormatter = NumberFormatter()
            numberFormatter.numberStyle = .currency

            // currency conversion
            let salesTax = numberFormatter.string(from: NSNumber(value: Float(taxes)))
            let totalFee = numberFormatter.string(from: NSNumber(value: Float(total)))


            // labels
            popUp.subtotalLbl.text = info

            popUp.salesTaxLbl.text = salesTax
            popUp.totalLbl.text = totalFee
        }
    }
}

This is not proper way to type cast String to Float, what you can do is create an extension to get a float value from the string like this,

extension String {
    var floatValue: Float {
        return (self as NSString).floatValue
    }
}

and use it like this :

let infoFloatValue = info.floatValue

let taxes = infoFloatValue * Float(0.0825)
let total = infoFloatValue + Float(taxes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM