gpt4 book ai didi

ios - 创建数组时发生 fatal error

转载 作者:行者123 更新时间:2023-11-30 13:40:22 25 4
gpt4 key购买 nike

这是我的代码。基本上,当用户首先选择餐饮质量时,是精致(20%)、休闲(15%)还是酒吧(11%)。然后,用户将被询问有关食品质量、服务等的 1-5 分等级。具体取决于他们选择的数字,例如,如果高级餐厅是 5/5,那么它将在已经 20 的基础上增加 4%百分比(每个数字都会有一个选定的值添加到小费中)。谁能弄清楚发生了什么事吗?或者是否有更好的方法。

import UIKit

@IBDesignable

class ViewController: UIViewController {



@IBOutlet weak var billTotal: UITextField!

var diningValue: Double = 0.0
@IBAction func toggleFineDining(sender: UIButton) {
sender.highlighted = sender.selected
if sender.selected == true{
diningValue = 0.20
}
}
@IBAction func toggleCasualDining(sender: UIButton) {
sender.highlighted = sender.selected
if sender.selected == true{
diningValue = 0.15
}
}
@IBAction func toggleBartender(sender: UIButton) {
sender.highlighted = sender.selected
if sender.selected == true{
diningValue = 0.11
}
}


@IBOutlet weak var service: UITextField!
@IBOutlet weak var friendliness: UITextField!
@IBOutlet weak var foodQuality: UITextField!
@IBOutlet weak var atmosphere: UITextField!
@IBOutlet weak var overall: UITextField!
@IBOutlet weak var recommend: UISwitch!
@IBOutlet weak var total: UILabel!



func totalBill(inputArray: [Double], value: Double, recommend: Bool)->Double{

var array: [Double] = []

let total = inputArray[0]

for var i = 1; i < inputArray.count; ++i {

array.append(inputArray[i] * 0.01)

}

let sum = array.reduce(0, combine: +)

var totalTip = Double()

if recommend == true{

totalTip = sum + value

} else {
if sum < 0.15 {
totalTip = 0.15
} else {
totalTip = sum + value
}
}

let tipValue = totalTip * total

return total + tipValue

}



@IBAction func calcTotal(sender: AnyObject) {
var valueList = [String(service.text), String(friendliness.text), String(foodQuality.text), String(atmosphere.text), String(overall.text)]

let list = doubleArray(valueList)



print(list)

let initialTotal = Double(billTotal.text!)

let total_bill = totalBill(list, value: diningValue, recommend: recommend.selected)

print("Total: ", total_bill)

print(String(format: "%.3f", Double(total_bill)))

total.text = "$" + String(format: "%.2f", Double(total_bill))

//diningValue = diningValue




}




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

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


}

func doubleArray(list: [String])->[Double]{

var doubledList: [Double] = []

for element in 0...list.count{

let doubledElement = Double(list[element])

doubledList.append(doubledElement!)

}

return doubledList

}

最佳答案

您的问题出在最后一个函数中:

func doubleArray(list: [String])->[Double]{

var doubledList: [Double] = []

for element in 0...list.count{

let doubledElement = Double(list[element])

doubledList.append(doubledElement!)

}

return doubledList

}

您的循环从 0 开始循环至list.count它崩溃于 list[element]elementlist.count (Out of Index)

您需要从 0...list.count 更改至0..<list.count :

func doubleArray(list: [String])->[Double]{

var doubledList: [Double] = []

for element in 0..<list.count{

let doubledElement = Double(list[element])

doubledList.append(doubledElement!)

}

return doubledList

}

我建议您以快速的方式做到这一点:

func doubleArray(list: [String])->[Double]{

var doubledList: [Double] = []

for element in list {
doubledList.append(Double(element) ?? 0.0)
}

return doubledList

}

使用 map 的较短版本功能:

func doubleArray(list: [String])->[Double]{
return list.map( { Double($0) ?? 0.0 })
}

关于ios - 创建数组时发生 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35638547/

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