当用户滚动选择器视图的两个部分(动态)时,如何将 Total1 和 Total2 值设置到标签中?

How To Set Total1 And Total2 Value Into Label When User Scrolls on Both Segment Of picker View (Dynamically)?

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    if component == 0 {
        lbl1.text = pdata[0][row]
        lbl1.adjustsFontSizeToFitWidth = true
        img1.image = UIImage(named : pdata[0][row])
        price1.text = priceData[row]
        total1 = Int(priceData[row]) ?? 0

        }

 else if component == 1 {
        lbl2.text = pdata[1][row]
        lbl2.adjustsFontSizeToFitWidth = true
        img2.image = UIImage(named : imgdata[row])
        price2.text = PriceData2[row]
        total2 = Int(PriceData2[row]) ?? 0

    }

   TotalPrice.text = String(total1! + total2!)



}

当用户在 1 Segment 上滚动时,总计变为 nil 并且 app 崩溃 我如何存储总计 1 和总计 2 变量以显示 price 的总和给用户

您可以使用合并运算符来防止强制展开:

TotalPrice.text = String((total1 ?? 0) + (total2 ?? 0))

https://en.wikipedia.org/wiki/Null_coalescing_operator