首先,你應該創建一個變量,你可以保存數據,然後返回。
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
}
或更容易:
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)
時產生的功能,在這裏你標記你的元組作爲lat
,第二個爲lon
的第一個元素的類型。
之後,你可以寫這樣的事情:
let (lat, lon) = getTowerCoordinates("Eiffel Tower")
print(lat)
print(lon)
或
let coordinates = getTowerCoordinates("Eiffel Tower")
print(coordinates.lat)
print(coordinates.lon)
這是相同