Swift:如何删除最后一个字符

时间:2018-07-23 21:36:25

标签: ios swift eureka-forms

我正在用尤里卡形式(服装行)做一个应用程序,问题是当我想用percentField计算firstValue时,计算不正确,因为当我尝试转换为double时,无法在double ex中转换%:
firstValue = 34
percentField = 9%
问题是“ 9%”不能转换为双精度值,值为0
percentField * firstValue = 0,因为无法转换%

代码:

func eingabessumeBruttoCalc(forPercentage isPercentage: Bool){
        let rowEingabessumeBrutto: KalkulationRow = (formViewController()?.form.rowBy(tag:"eingabessumeBrutto"))!
       let rowRabatt: KalkulationRow = (formViewController()?.form.rowBy(tag:"rabatt"))!
        let rowSkonto: KalkulationRow = (formViewController()?.form.rowBy(tag:"skonto"))!
        let rowDiverse: KalkulationRow = (formViewController()?.form.rowBy(tag:"diverse"))!
        let rowZielBrutto:KalkulationRow = (formViewController()?.form.rowBy(tag:"zielBrutto"))!
        let rowZielNetto:KalkulationRow = (formViewController()?.form.rowBy(tag:"zielNetto"))!
        let rowEingabessumeNetto:KalkulationRow = (formViewController()?.form.rowBy(tag:"result"))!

        let ValueEingabessumeBrutto = Double(rowEingabessumeBrutto.cell.valueField.text ?? "") ?? 00
        let ValueEingabessumeNetto = Double(rowEingabessumeNetto.cell.valueField.text ?? "") ?? 00


        let ValueZielBrutto = Double(rowZielBrutto.cell.percentageField.text ?? "")  ?? 00
        let totalZielBrutto = ValueEingabessumeBrutto * ValueZielBrutto * 0.01
        rowZielBrutto.cell.valueField.text = total.description

        let ValueRabatt = Double(rowRabatt.cell.percentageField.text ?? "")  ?? 00
        let totalRabatt = ValueEingabessumeBrutto * ValueRabatt * 0.01
        rowRabatt.cell.valueField.text = total.description

        let SkontoValue = Double(rowSkonto.cell.percentageField.text ?? "")  ?? 00
        let rowsSkonto = Double(rowEingabessumeBrutto)-Double(rowRabatt)
        let totalSkonto = rowSkonto * SkontoValue * 0.01
        rowSkonto.cell.valueField.text = total.description

        let ValueDiverse = Double(rowDiverse.cell.percentageField.text ?? "")  ?? 00
        let totalDierse = ValueEingabessumeBrutto * ValueDiverse * 0.01
        rowDiverse.cell.valueField.text = total.description

        let ValueZielNetto = Double(rowZielNetto.cell.percentageField.text ?? "")  ?? 00
        let totalDierse = ValueEingabessumeNetto * ValueZielNetto * 0.01
        rowZielNetto.cell.valueField.text = total.description
      if let text = percentageField.text,
            !text.isEmpty {
            percentageField.text?.removeLast()

        }
     

这就是我的尝试。

我要检查最后一个字符是否为%,请将其删除。 预先感谢

1 个答案:

答案 0 :(得分:1)

  

我要检查最后一个字符是否为%,请将其删除。预先感谢

if let text = textField.text, let lastChar = text.last, lastChar == "%" {
    textField.text = String(text.dropLast())
}