在Playground编译好的编译器错误

时间:2015-08-13 18:28:26

标签: ios swift

我有以下代码,如果在操场上检查符合OK(除了动作触发的功能)......但在我的应用程序中我得到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错误。

1 个答案:

答案 0 :(得分:0)

我会放弃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
    }
}