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

Tim Wiklund
Tim Wiklund
746 Points

Need help with challenge task 1 of 1, 2.0 function.

functions.swift
func getTowerCoordinates(location: String) -> cordination: Double {

    // 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

    var cordination: Double

    switch location {

    case "Eiffel Tower": cordination = 48.8582; 2.2945
    case "Great Pyramid": cordination = 29.9792; 31.1344
    case "Sydney Opera House": cordination = 33.8587; 151.2140
    default: cordination = 0

    return (cordination)

    }
}

getTowerCoordinates("Eiffel Toweer")

2 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

In this assignment, you are asked to return a location, which consists of two values, latitude and longitude. This is where tuples come in handy, because they let you return multiple values from a function. (You would still use a framework struct like CLLocation, but for the sake of this assignment it's a tuple)

In your code, you are returning a single Double, when you really need to return a tuple consisting of two Doubles. See the following code and annotations.

functions.swift
// The return type is a tuple, consisting of two Doubles
func getTowerCoordinates(location: String) -> (Double, Double) {

    // 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

    // Again, cordination is now a tuple (Double, Double)
    var cordination: (Double, Double)

    // Tuples are assigned as below
    switch location {
    case "Eiffel Tower": cordination = (48.8582, 2.2945)
    case "Great Pyramid": cordination = (29.9792, 31.1344)
    case "Sydney Opera House": cordination = (33.8587, 151.2140)
    default: cordination = (0, 0)
    }

    // The return statement should really be the last thing of this function.
    // You had it in the switch statement, where only case and default belong.
    // (You could also return values from case/default, but what you did is considered
    // best practice, that is having only one return statement at the bottom)
    return cordination
}

// You had a typo here, "Eiffel Toweer", although
// this was not the reason for the check not to pass
getTowerCoordinates("Eiffel Tower")

As always: please paste your code in a playground, it offers code completion and helps with meaningful warnings and errors.

Hope that helps :)

<small>

Disclaimer: With every post, paste your code and explain what you are having problems with. - If you are getting console errors, post them here as well. - Use an Xcode Playground for coding, it offers autocompletion and displays meaningful warnings and errors. - If code check fails, check the preview button for any errors. - Upvote and/or select a working solution as best answer. Thanks! </small>

Tim Wiklund
Tim Wiklund
746 Points

Thanks for the quick respons and good description