这是我的代码。基本上当用户首先选择餐饮质量时,无论是罚款(20%),休闲(15%)还是酒吧(11%)。然后,用户将以1-5的比例询问用户的食品质量,服务等。根据他们选择的数量,例如,如果精美的餐厅是5/5那么它将比已经20%百分比(每个数字将有一个选定的值添加到提示)。谁能弄明白发生了什么?或者如果有更好的方法可以做到这一点。
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
}
答案 0 :(得分:0)
您的问题出在上一个功能中:
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]
为element
list.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 })
}