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 2.0 Functions Function Parameters Returning Complex Values

Sheng Wei
Sheng Wei
4,382 Points

[Help!] Function with Switch statements, tuples and return values

Task: Declare a function named getTowerCoordinates that takes a single parameter of type String, named location, and returns a tuple containing two Double values. Use a switch statement to switch on the string passed in to return the right set of coordinate values for the location.

Eiffel Tower - lat: 48.8582, lon: 2.2945 | Great Pyramid - lat: 29.9792, lon: 31.1344 | Sydney Opera House - lat: 33.8587, lon: 151.2140

Input: "Eiffel Tower" Desired Function Output: (48.8582, 2.2945)

I think my real problem is that Im not sure which terms initialise, switch and return. Would really appreciate any help!

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

    var location: String,lat: Double,lon: 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: location = "Dont know"

    }

    return (lat, lon)

}

1 Answer

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

You only have to make a few changes.

The challenge won't accept a named tuple for the return value, so you need to change "(lat: Double, lon: Double)" to "(Double, Double)" in the function header.

You don't need the first line of code inside your function ("var location: String,lat: Double,lon: Double"), so you can delete that.

Inside the switch statement, you need to change the format of your default case - the challenge asks you to return (0, 0) for the default case, so you should replace that line with "default: return (0, 0)".

After that change is made, you don't need the last line of code inside your function ("return (lat, lon)"), so you can delete that.

func getTowerCoordinates (location: String) -> (lat: Double, lon: Double) { // CHANGE RETURN VALUE TO (Double, Double)

    var location: String,lat: Double,lon: Double // DELETE THIS LINE

    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: location = "Dont know" // CHANGE DEFAULT STATEMENT TO return (0, 0)

    }

    return (lat, lon) // DELETE THIS LINE

}

I hope this helps!

Sheng Wei
Sheng Wei
4,382 Points

Thanks Anjali, couldn't have asked for a better answer! :)