Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Eliot Murton
Eliot Murton
7,315 Points

Xcode Swift issue with initializing.

I am creating the model for a calculator and Xcode keeps telling me that for the init method that I cannot invoke BinaryOperation/UnaryOperation with the relevant Enum cases. Why is this. It is really confusing me.

class CalculatorBrain {

    enum Op {
        case Operand(Double)
        case UnaryOperation(String, Double -> Double)
        case BinaryOperation(String, (Double -> Double) -> Double)
    }

    var opStack = [Op]() 
    var knownOps = [String:Op]() 

    init() {
        knownOps["×"] = Op.BinaryOperation("×") { $0 * $1 }
        knownOps["÷"] = Op.BinaryOperation("÷") { $1 / $0 }
        knownOps["+"] = Op.BinaryOperation("+") { $0 + $1 }
        knownOps["−"] = Op.BinaryOperation("−") { $1 - $0 }
        knownOps["√"] = Op.UnaryOperation("√") { sqrt($0) }
    }

    func pushOperand(operand: Double) {
        opStack.append(Op.Operand(operand))
    }

    func performOperation(symbol: String) {

    }
}

2 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

This will resolve your issue, but not sure if it will make your code work as you intend to.

class CalculatorBrain {

    enum Op {
        case Operand(Double)
        case UnaryOperation(String, Double -> Double)
        case BinaryOperation(String, (Double, Double) -> Double)
    }

    var opStack = [Op]()
    var knownOps = [String:Op]()

    init() {
        knownOps["×"] = Op.BinaryOperation("×") {$0 * $1}
        knownOps["÷"] = Op.BinaryOperation("÷") {$1 / $0}
        knownOps["+"] = Op.BinaryOperation("+") {$0 + $1}
        knownOps["−"] = Op.BinaryOperation("−") {$1 - $0}
        knownOps["√"] = Op.UnaryOperation("√")  {sqrt($0)}
    }

    func pushOperand(operand: Double) {
        opStack.append(Op.Operand(operand))
    }

    func performOperation(symbol: String) {

    }
}
Eliot Murton
Eliot Murton
7,315 Points

Im sorry. I think I am being slow and missing something obvious. What is it you changed? I copy and pasted it in and it works but I am stumped at what you did.

Jhoan Arango
Jhoan Arango
14,575 Points

This is one of those things that will drive anyone crazy trying to figure out what went wrong with your code.

Look at the tuple in your enum.

case BinaryOperation(String, (Double ->Double) -> Double)

case BinaryOperation(String, (Double, Double) -> Double) // here is the change.