1

I couldn't solve an "if let" issue in my code. Based on the error it says that I must use an optional type?

@IBAction func operate(sender: UIButton) {

    if userIsInTheMiddleOfTypingANumber{
        enter()
    }
    if let operation = sender.currentTitle {
        if let result = calcModel.performOperation(operation){
            displayValue = result
        }else {
            displayValue = 0
        }
    }
}
Phiter
  • 14,570
  • 14
  • 50
  • 84
Mohammad A
  • 11
  • 3

1 Answers1

1

I suspect that your problem is in this line:

if let result = calcModel.performOperation(operation)

Your performOperation function's return type is (), or void, which means it doesn't return anything. Chances are, to get the error in the title, your performOperation function on calcModel has the following signature:

func performOperation(operation: String) {

Note the lack of an arrow and type. If you're going to return a result, the signature should be something like this:

func performOperation(operation: String) -> Int {

You would only need the if let, if you decided to return Int?.

Josh Heald
  • 3,907
  • 28
  • 37
  • Thanks Josh! I think I spotted the error. I forget to make performOperation to return the value. – Mohammad A Mar 10 '16 at 18:22
  • You're welcome, and welcome to Stack Overflow . When an answer solves your issue, you can mark it as the accepted answer using the grey tick button next to it, and some people choose to up vote as well. – Josh Heald Mar 10 '16 at 18:25