gpt4 book ai didi

ios - 我试图让输入的数据进行计算并显示在标签中

转载 作者:行者123 更新时间:2023-11-28 19:25:24 25 4
gpt4 key购买 nike

我正在尝试获取添加到主屏幕标签中显示的信息。我想让它计算利润,但我收到错误消息“‘String’类型的值没有成员‘text’”

@IBAction func addButtonTapped(_ sender: Any) {


let alert = UIAlertController(title: "Product Information", message: nil, preferredStyle: .alert)
alert.addTextField(configurationHandler: { (itemTextField) -> Void in
itemTextField.placeholder = "Item"
})
alert.addTextField(configurationHandler: { (priceTextField) -> Void in
priceTextField.placeholder = "Price"
})
alert.addTextField(configurationHandler: { (salePriceTextField) -> Void in
salePriceTextField.placeholder = "Sale Price"
})



alert.addAction(UIAlertAction(title: "Add", style: .default) { (action) in
let item = alert.textFields?[0].text ?? ""
let price = alert.textFields?[1].text ?? ""
let salesPrice = alert.textFields?[2].text ?? ""

let itemPriceLabel = Double(price.text!)
let salePriceLabel = Double(salesPrice.text!)

if itemPriceLabel != nil && salePriceLabel != nil {

let profitValue = Double (itemPriceLabel! - salePriceLabel!)
ProfitLabel.text = profitValue
}

let product = Product(item: item, price: price, salesPrice: salesPrice)
self.addProduct(product)


})

self.present(alert, animated: true, completion: nil)
storeData()


}

最佳答案

您的 price 和 salesPrice 变量已经仅为字符串。但是您已尝试在下一条语句中访问 String 类中不存在的那些变量中的文本字段。

let itemPriceLabel = Double(price)
let salePriceLabel = Double(salesPrice)

并按如下方式分配您的值。

ProfitLabel.text = String(profitValue)

关于ios - 我试图让输入的数据进行计算并显示在标签中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59232587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com