gpt4 book ai didi

ios - 在 Playground 中编译正常的编译器错误

转载 作者:行者123 更新时间:2023-11-30 14:08:51 26 4
gpt4 key购买 nike

我有以下代码,如果在 Playground 中检查,则符合正常要求(除了由操作触发的函数)...但在我的应用程序中,我遇到了 7 个编译错误。

我的代码:

import UIKit

class LocationViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {


@IBOutlet weak var DisplayStartMileage: UITextField!
@IBOutlet weak var labelFuelAmount: UILabel!
@IBOutlet weak var spinFuelAmount: UIPickerView!

var mileageToPass: String!

var fuelAmount: Double = 0.00

var poundValues = [String]()
var penceValues = [String]()

for var indexP:Int = 0; indexP < 100; indexP += 1 {
poundValues.append("£ \(indexP)")
penceValues.append(NSString(format: ".%02d", indexP) as String)
}

let pickerData = [poundValues, penceValues]


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
DisplayStartMileage.text = mileageToPass;

spinFuelAmount.delegate = self
spinFuelAmount.dataSource = self

}

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

@IBAction func FuelSegmentChanged(sender: UISegmentedControl) {

switch sender.selectedSegmentIndex
{
case 0:
labelFuelAmount.hidden = false
spinFuelAmount.hidden = false

case 1:
labelFuelAmount.hidden = true
spinFuelAmount.hidden = true

default:
break;
}

}

func numberOfComponentsInPickerView(spinFuelAmount: UIPickerView) -> Int {
return pickerData.count
}

func pickerView(spinFuelAmount: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData[component].count
}

func pickerView(spinFuelAmount: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[component][row]
}

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

let fuelAmount = Int(pickerData[0][spinFuelAmount.selectedRowInComponent(0)].stringByReplacingOccurrencesOfString("£ ", withString: "")) + Int(pickerData[1][spinFuelAmount.selectedRowInComponent(1)].stringByReplacingOccurancesOfString(".", withString: ""))/100

}

}

现在我知道 SO 不适用于调试,但我不知所措...大多数代码都符合 Playground,但许多行确实符合我的 ViewController 类中的 Playground 错误。

最佳答案

我会放弃将 poundValuespenceValues 作为成员属性,并在 viewDidLoad() 中设置所有这些内容:

var pickerData = [[String]]()

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.
DisplayStartMileage.text = mileageToPass;

var poundValues = [String]()
var penceValues = [String]()
for var indexP:Int = 0; indexP < 100; indexP += 1 {
poundValues.append("£ \(indexP)")
penceValues.append(NSString(format: ".%02d", indexP) as String)
}
self.pickerData = [poundValues, penceValues]

spinFuelAmount.delegate = self
spinFuelAmount.dataSource = self
}

更新

以下是对该 pickerView(_:didSelectRow:inComponent) 方法的建议:

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

var firstComponent = pickerData[0][spinFuelAmount.selectedRowInComponent(0)]
firstComponent = firstComponent.stringByReplacingOccurrencesOfString( "£ ", withString: "" )

var secondComponent = pickerData[1][spinFuelAmount.selectedRowInComponent(1)]
secondComponent = firstComponent.stringByReplacingOccurrencesOfString( "£ ", withString: "" )

if let firstValue = Int(firstComponent), let secondValue = Int(secondComponent) {
let fuelAmount = firstValue + secondValue / 100
}
}

关于ios - 在 Playground 中编译正常的编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31995814/

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