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

Returning Complex Values

Hey guys, Could you tell me what is wrong with my code?

Thanks.

functions.swift
// Enter your code below
func coordinates (location: String) -> (lat: Double, lon: Double) {

  if (location = "Eiffel Tower") {
  return coordinates(48.8582, 2.2945)

  } else if (location = "Great Pyramid") {
  return coordinates(29.9792, 31.1344)

  } else if (location = "Sydney Opera House") {
  return coordinates(33.8587, 151.2140)

   } else {
    return coordinates(0.0,0.0)
    }


}

2 Answers

Thomas Dobson
Thomas Dobson
7,511 Points

Hi Han,

You are pretty close, some some slight changes:

// Enter your code below

//declare your function input as internal string for, external location
//also for the return statement, this Code Challenge wont let you name your tuple
//however note I named mine as well for readability. 
func coordinates(for location: String) -> (Double, Double) {

    if (location == "Eiffel Tower") { //You were setting location to eiffel tower, use the comparative operator as seen here.
        return (48.8582, 2.2945) //remove coordinates. You only want to return the Tuple (in this case the lat, long)

    } else if (location == "Great Pyramid") { //You were setting location to great pyramid, use the comparative operator as seen here.
        return (29.9792, 31.1344) //remove coordinates. You only want to return the Tuple (in this case the lat, long)

    } else if (location == "Sydney Opera House") { //You were setting location to sydney opera house, use the comparative operator as seen here.
        return (33.8587, 151.2140) //remove coordinates. You only want to return the Tuple (in this case the lat, long)

    } else {
        return (0.0,0.0)
    }


}

Also note another way to acomplish this task, which looks a and reads much better using switch a statement

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

I hope this helps

Thanks so much!

Anthia Tillbury
Anthia Tillbury
3,388 Points

I have this, but it is not accepted. Is this valid:

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

    var lat: Double = 0.0
    var lon: Double = 0.0

    switch location {
    case "Eiffel Tower": lat = 48.8582; lon = 2.2945
    case "Great Pyramid": lat = 29.979; lon = 31.1344
    case "Sydney Opera House": lat = 33.8587; lon = 151.2140
    default: 0.0
    }

    return (lat, lon)

}


let eiffelTower = coordinates(for: "Eiffel Tower")

eiffelTower