iOS Functions in Swift Adding Power to Functions Returning Complex Values

Sam Belcher
Sam Belcher
3,719 Points

Returning Complex Values

I'm confused, not sure how to fix my mistake :/

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

  var place: Double = []

  switch location {
  case "Eiffel Tower": place = [48.8582, 2.2945]
  case "Great Pyramid": place = [29.9792, 31.1344]
  case "Sydney Opera House": place = [33.8587, 151.2140]
  default: place = [0.0, 0.0]
 }
 return(place)
}
JLN CRML
JLN CRML
30,233 Points

Hi, you try to assign an empty array to a variable of type Double, which is not possible. You also might rather want to use Tuples here, since they always have two values, perfectly fitting for coordinates.

Swift Documentation - Types

I am showing you below two methods of solving this without changing the signature, either with Tuples, which I would recommend, or Arrays.

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

    var place: (Double, Double)

    switch location {
    case "Eiffel Tower": place = (48.8582, 2.2945)
    case "Great Pyramid": place = (29.9792, 31.1344)
    case "Sydney Opera House": place = (33.8587, 151.2140)
    default: place = (0.0, 0.0)
    }
    return(place)
}
func coordinates(for location: String) -> (Double, Double) {

    var place: [Double] = []

    switch location {
    case "Eiffel Tower": place = [48.8582, 2.2945]
    case "Great Pyramid": place = [29.9792, 31.1344]
    case "Sydney Opera House": place = [33.8587, 151.2140]
    default: place = [0.0, 0.0]
    }
    return(place[0], place[1])
}