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

Ruth Yoel
Ruth Yoel
986 Points

Returning a tuple

I wrote the code on Xcode and produced a tuple, but when I copied it to the editor window, I received a bummer informing me that my function needs to produce a tuple. Can you help showing me what I did wrong?

Thanks a lot!

Ruth

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

    var lat: Double = (0.0)
    var lon: Double = (0.0)

    if location == "Eiffel Tower"{
            lat = (48.8582)
            lon = (2.2945)
    } else if location == "Great Pyramid"{
            lat = (29.9792)
            lon = (31.1344)
    } else if location == "Sydney Opera House"{
            lat = (33.8587)
            lon = (151.2140)
    } else  {
        lat = (0.0)
        lon = (0.0)
    }

  return (lat, lon)
}

5 Answers

The directions say you do not have to name the return values so func coordinates(for location: String) -> (Double, Double) you must remove lat and lon from the return values

Ruth Yoel
Ruth Yoel
986 Points

Thanks a lot!

Ben Shockley
Ben Shockley
6,094 Points

In the header of your function, where you declared the return type, you don't need the titles inside the tuple, (the lat: and the lon: part). When I removed those, it passed without and issue.

Probably, for whatever test they are using to check them answer, the titles are throwing it off in the return.

So your function header should look like this.

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

Hope that helps.

-- Ben

Ruth Yoel
Ruth Yoel
986 Points

Thanks a lot!

Essentially you got it almost correct. However try using a switch statement and make sure you are returning touple as return types specifies "(lat: Double, lon: Double)".

This esentially what the editor is looking for.

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

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

    switch location {
    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: return (0,0)
    }

return (lat, lon)
}
Ben Shockley
Ben Shockley
6,094 Points

I just tried to run your code and it didn't work either, because the tuple has the titles in it.

Ruth Yoel
Ruth Yoel
986 Points

Thanks a lot!

Ruth Yoel
Ruth Yoel
986 Points

Thanks a lot!

Ruth Yoel
Ruth Yoel
986 Points

That's very logical. It works. Thanks a lot!

Ruth Yoel
Ruth Yoel
986 Points

It worked! Thank you very much!