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

Kjartan Ottosson
Kjartan Ottosson
809 Points

I have no clue why this isnยดt working, can you help me?

Hi, kinda lost at the moment. Had a look at some community answers but still no luck. Can you help?

Regards, Kjartan

functions.swift
// Enter your code below

func coordinates (for location: String) -> (Double,Double) {
      var lat: Double = 0,0 lon: double = 0,0

      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 (lat = 0.0, lon = 0.0)
        }

  return(lat,lon)

}

coordinates("Eiffel Tower")
Ulan Assanoff
Ulan Assanoff
9,922 Points

Just try in this way

func coordinates (for location: String) -> (Double,Double) {
    let _: Double = 0 ,_: Double = 0
       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 (lat: 0.0, lon: 0.0)
    }


}

coordinates(for: "Eiffel Tower")

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Kjartan,

You are on the right track, but there are a few things going wrong here.

  • First, the challenge didn't ask for you to declare any variables (You have lat and lon. Also, with this line, the syntax is incorrect. If you were to declare those variables, they would need to be declared separately. You cannot declare two different variables with only one var keyword. (Maybe have a quick review on variables.)
  • Second, also has to do with the lat and lon variables you are using. The challenge asks that you return the values, but that you don't have to name the return values. Inside of your switch statement you are using names.
  • Third, you have to return the values based on which case matches inside the switch statement (which you are doing), so you can't return anything outside of the switch, as there would be nothing to return.
  • Lastly, you are calling the function. The challenge did not ask for a function call, so although this is correct syntax, it will cause the challenge to fail. Challenges are very picky and extremely specific. If you add something that wasn't asked for, it will more often than not throw a Bummer.

I don't normally give the corrected code outright, but I have provided it below for you to review and compare. Have a look, and I hope it all makes sense for you.

func coordinates (for location: String) -> (Double,Double) {
  switch location {
  case "Eiffel Tower": 
    return (48.8582, 2.2945)
  case "Great Pyramid": 
    return (29.9792, 31.1344)
  case "Sydney Opera House": 
    return (33.8587, 151.2140)
  default: 
    return (0.0, 0.0)
  }
}

Keep Coding! :) :dizzy: