Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift 2.0 Functions Function Parameters Returning Complex Values

Functions with switch statement... I'm having trouble with my code I'm trying to return lat and lon but its not working

func getTowerCoordinates(amousLandmark: String) ->(Double, Double) {

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

}

return (lat,lon) }

functions.swift
// Enter your code below

func getTowerCoordinates(location: String) ->(Double, Double)  {
  switch amousLandmark {
    case "Eiffel Tower": (lat: 48.8582, lon: 2.2945)
    case "Great Pyramid": (lat: 29.9792, lon: 31.1344)
    case "Sydney Opera House": (lat: 33.8587, lon: 151.2140)
      default: 0 , 0

  } 

return (lat,lon)
}
Mazen Halawi
Mazen Halawi
7,806 Points

Add the return on the case statement

case "Eiffel Tower": return (lat: 48.8582, lon: 2.2945)

Do that for the rest. Im typing on an ipad so i cant type everything. ;)

1 Answer

Keli'i Martin
Keli'i Martin
8,227 Points

First off, you are trying to switch off a variable that isn't defined anywhere. amousLandmark doesn't exist anywhere in the scope of your function. You should be switching off the location parameter.

Second, in your switch statement, for each case you're using lat and lon, but those are variables that are not defined. Since the return tuple is unnamed, you don't need to set the values of lat and lon to anything before returning. You could simply return the tuple of coordinates right from each case. For example:

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)
}

Hope this helps!

Keli'i Martin
Keli'i Martin
8,227 Points

I guess leaving the lat: and lon: on those values works also, but definitely changing amousLandmark to location is needed.

Hey thanks a ton this helped out a lot a guess i had kind of a syntax error i need to pay attention to details... I was way to sure of myself thanks Keli

that and the i did not realize i should be using a return for each case statement but it makes sense for this particular situation

Keli'i Martin
Keli'i Martin
8,227 Points

Not a problem! There are certainly a number of different ways to accomplish the same thing. Using returns in each case statement is just one approach. You were on the right track, so keep it up!