gpt4 book ai didi

ios - 在 Swift 中更改类型

转载 作者:行者123 更新时间:2023-12-01 16:12:19 25 4
gpt4 key购买 nike

我是 Swift 的新手(隔离学习),并且正在跟随 YouTube 视频创建一个总价应用程序。我在将字符串更改为 double 类型时遇到了麻烦,基本上是将输入转换为 double 类型,以便我可以用它进行数学运算。

这是我的代码:

import UIKit


class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@IBOutlet weak var pricetxt: UITextField!
@IBOutlet weak var taxtxt: UITextField!
@IBOutlet weak var totalpricelbl: UILabel!

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func Calculate(sender: AnyObject) {
let price = Double(pricetxt.text!)!
let tax = Double(taxtxt.text!)!

let totalsalestax = price * tax
let totalprice = price + totalsalestax
totalpricelbl.text = "$\(totalprice)"
}

}


我收到这两行错误:
let price = Double(pricetxt.text!)!
let tax = Double(taxtxt.text!)!

说:

"Cannot invoke 'init' with an argument of type '@!value String!'



在我的 Mac 10.9.5 上运行
XCode 是 6.2
我知道这些都是旧的,但我无法升级我的笔记本电脑。

任何帮助表示赞赏!

最佳答案

试试这个:

let price = (pricetxt.text! as NSString).doubleValue
let tax = (taxtxt.text! as NSString).doubleValue

关于ios - 在 Swift 中更改类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61858482/

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