Reputation: 3
I am learning to write Swift 2.0 code with treehouse. I am trying to write a code where you would enter either "Eiffel Tower," "Great Pyramid," "Sydney Opera House" and the function would output the longitude and latitude coordinates. I am having trouble having the function return the coordinates - any help would be appreciated!
func getTowerCoordinates (location: String) -> (lat: Double, lon: Double) {
switch location {
case "Eiffel Tower": (48.8582, 2.2945)
case "Great Pyramid": (29.9792, 31.1344)
case "Sydney Opera House": (33.8587, 151.2140)
default: (0,0)
}
return (lat, lon)
}
Upvotes: 0
Views: 232
Reputation: 584
First, you should create a variable where you could save the data and then return it.
func getTowerCoordinates (location: String) -> (lat: Double, lon: Double) {
var coordinates: (lat: Double, lon: Double)
switch location {
case "Eiffel Tower":
coordinates = (48.8582, 2.2945)
break
case "Great Pyramid":
coordinates = (29.9792, 31.1344)
break
case "Sydney Opera House":
coordinates = (33.8587, 151.2140)
break
default:
coordinates = (0,0)
}
return coordinates
}
or easier:
func getTowerCoordinates (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:
return (0, 0)
}
}
(lat: Double, lon: Double)
is resulting type of the function, where you tag your first element of the tuple as lat
and the second one as lon
.
Afterwards, you can write something like that:
let (lat, lon) = getTowerCoordinates("Eiffel Tower")
print(lat)
print(lon)
or
let coordinates = getTowerCoordinates("Eiffel Tower")
print(coordinates.lat)
print(coordinates.lon)
which is the same
Upvotes: 1