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 Functions in Swift Adding Power to Functions Returning Complex Values

Not sure how to work the switch statement here!

I'm not able to get my head around this. When I try returning (if I'm doing it right) it runs thousands of times! Would appreciate some help here!

functions.swift
func coordinates(for location:String) -> (Double, Double) {

    switch location {
    case "EiffelTower" : (lat: 48.8582, lon: 2.2945)
    case "Great Pyramid" : (lat: 9.9792, lon: 31.1344)
    case "Sydney Opera House" : (lat: 33.8587, lon: 151.2140)
    default : (0, 0)
}

    return coordinates(for: location)
}

//coordinates(for: "eiffelTower")

1 Answer

andren
andren
28,558 Points

The reason it runs thousands of times is because the thing you are returning is a call to the function itself, which means that you are trying to return what is returned from your function. But since the thing returned from your function is a call to your function, your function ends up calling itself over and over endlessly.

In order to return the coordinate values in the switch statement you simply need to add return in front of them like this:

func coordinates(for location:String) -> (Double, Double) {

  switch location {
      case "Eiffel Tower" : return(lat: 48.8582, lon: 2.2945)
      case "Great Pyramid" : return(lat: 29.9792, lon: 31.1344)
      case "Sydney Opera House" : return(lat: 33.8587, lon: 151.2140)
      default : return(0, 0)
  }
}

You also had two typos in your code, you spelled "Eiffel Tower" as "EiffelTower" and missed a "2" in the lat coordinates of the Great Pyramid. I fixed both of those typos in the example I posted above.

Thank you so much for both the explanation and demonstration. Much appreciated!