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 Swift Closures Functions as First Class Citizens Higher Order Functions

higherOrderFunctions.swift solution parsing

Hello, So the following code displays the expected result in the XCode Playground but doesn't pass the work checking algorithm on the teamtreehouse website. Is XCode cheating in my favor or am I missing something?

higherOrderFunctions.swift
/** 
  For this code challenge, let’s define a math operation as a function that 
  carries out some work on two integers and returns an integer as well. An 
  example is the function below, `differenceBetweenNumbers`, which takes two 
  integers and calculates the difference between the numbers. After calculating, 
  it returns the difference.
*/

func differenceBetweenNumbers(a: Int, b:Int) -> (Int) {
  return a - b
}

// Enter your code below
func mathOperation(f: (Int,Int) -> (Int),x:Int, y:Int) -> Int {
    return f(x,y)
}
let difference = mathOperation(differenceBetweenNumbers,6,3)
print(difference)

2 Answers

Mohammad Akbar Abdul Latip
Mohammad Akbar Abdul Latip
2,288 Points

Hi guys. I found the solution at https://teamtreehouse.com/forum/cant-properly-nest-two-swift-functions. Details to solution explained by Taha who got the code to work.

This did it! Thanks for the cross post.

func differenceBetweenNumbers(a: Int, b:Int) -> (Int) { return a - b }

// Enter your code below func mathOperation(mathOp: (Int, Int) -> Int, a: Int, b: Int) -> Int { return mathOp(a, b) }

let difference = mathOperation(differenceBetweenNumbers, 26, 22)

Thanks for the reply. While what you provided works in XCode, for some reason the code challenge still says it is incorrect.

The provided hint is "Bummer! Make sure you are computing the difference between two integers using the functions created and assigning the result to a constant named difference."

We are both doing exactly what the hint suggests:

Your line: let difference = mathOperation(differenceBetweenNumbers, 26, 22)

My line: let difference = mathOperation(differenceBetweenNumbers,6,3)

I reached out to tech support but they mentioned posting here. Any other ideas?