Swift:缺少参数错误

时间:2015-10-02 02:19:44

标签: swift swift2

我在尝试跟随第一次斯坦福讲座时遇到了问题。我假设这是因为我正在使用swift 2 ....这是刚刚发布的。 (而且课程不是)下面我展示了我遇到错误的地方。在课堂上,"输入"计算器上的按钮的代码为@IBAction func enter() {,但我的代码为@IBAction func enter(sender: UIButton) {

将按钮放入我的代码时,这是我做错了吗?有什么方法可以解决?如果我能澄清任何事情,请告诉我。

import UIKit

class ViewController: UIViewController
{

    @IBOutlet var display: UILabel!

    var userIsInTheMiddleOfTypingANumber: Bool = false

    @IBAction func appendDigit(sender: UIButton) {
        let digit = sender.currentTitle!
        if userIsInTheMiddleOfTypingANumber {
            display.text = display.text! + digit
        } else {
            display.text = digit
            userIsInTheMiddleOfTypingANumber = true
        }
    }

    @IBAction func operate(sender: UIButton) {
        let operation = sender.currentTitle!
        if userIsInTheMiddleOfTypingANumber {
            enter() // Here is my error: *Missing Argument for parameter on the line of code that says enter()
        }
        switch operation {
            case "x":
                if operandStack.count >= 2 {
                    displayValue = operandStack.removeLast() * operandStack.removeLast()
                    enter() // Here is my error:  Again... 
                }

            default: break
        }
    }

    var operandStack = Array<Double>()



    @IBAction func enter(sender: UIButton) {
        userIsInTheMiddleOfTypingANumber = false
        operandStack.append(displayValue)
        print("operandStack = \(operandStack)")
    }

    var displayValue: Double {
        get {
            return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
        }
        set {
            display.text = "\(newValue)"
            userIsInTheMiddleOfTypingANumber = false
        }
    }
}

1 个答案:

答案 0 :(得分:0)

enter需要输入参数,但您没有提供输入参数。将其替换为enter(sender)

@IBAction func operate(sender: UIButton) {
    let operation = sender.currentTitle!
    if userIsInTheMiddleOfTypingANumber {
        enter(sender)
    }

    switch operation {
        case "x":
            if operandStack.count >= 2 {
                displayValue = operandStack.removeLast() * operandStack.removeLast()
                enter(sender)
            }

        default: break
    }
}