计算器中的分段控制

Segmented Control in Calculator

我试图制作一个包含两个值的分段控件,但我在制作时遇到了问题。我想将输入乘以在分段视图中选择并显示在标签中的任何一侧,但我将 运行 保留为错误。有什么帮助吗?

@IBOutlet var input: UITextField!
@IBOutlet var output: UILabel!
@IBOutlet var controller: UISegmentedControl!
@IBAction func convert(sender: UIButton) {
}






@IBAction func change(sender: AnyObject) {
    if controller.selectedSegmentIndex == 0 {
       result = number1 * 1
    }
    if controller.selectedSegmentIndex == 1 {
        result = number1 * 5
    }



    let number1 = Double(input.text!)
    label.text = "\(result)"

}

为什么在声明前使用"number1"? 试试这个:

@IBOutlet var input: UITextField!
@IBOutlet var output: UILabel!
var result:Double! //fixed "cannot be applied to Double and Int." problem
@IBOutlet var controller: UISegmentedControl!
@IBAction func convert(sender: UIButton) {
}


@IBAction func change(sender: AnyObject) {
let number1 = Double(input.text!)
    if controller.selectedSegmentIndex == 0 {
       self.result = number1 * 1
    }
    if controller.selectedSegmentIndex == 1 {
        self.result = number1 * 5
    }




    label.text = "\(result)"

}