iOS Functions in Swift Adding Power to Functions Returning Complex Values

Jamil Jones
Jamil Jones
10,852 Points

What I'm a doing wrong here? This item will not pass.

I am following the direction exactly how it states. But it will not pass. What am I missing??

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

functions.swift
func coordinates(for location:String) -> (Double, Double) {
}

1 Answer

Lucian Rotari
Lucian Rotari
12,007 Points

Hey Jamil, there is nothing wrong with your code, you just didn't finish the challenge.

Let have a look, they ask you to declare a function and that's what you have done. What you missed is that your function MUST return a value.

          func coordinates(for location:String) -> (Double, Double) {}
         // -> (Double,Double) means that this function MUST return a tuple-type 

Now the action is required here "For example, if I use your function and pass in the string "Eiffel Tower" as an argument, I should get (48.8582, 2.2945) as the value. If a string is passed in that doesn’t match the set above, return (0,0)"

So you have 3 locations

  1. Eiffel Tower
  2. Great Pyramid
  3. Sydney Opera House

Each of them has latitude and longitude:

  1. Eiffel Tower - lat: 48.8582, lon: 2.2945
  2. Great Pyramid - lat: 29.9792, lon: 31.1344
  3. Sydney Opera House - lat: 33.8587, lon: 151.2140

So your function must return the lat and lon for the location that user required.

For example if user required latitude and longitude for "Effel Tower" your function should return this values : (48.8582 , 2.2945)

In this case your function should look like that:

func coordinates(for location:String) -> (Double, Double) {
     if location == "Eiffel Tower" {
          retrun (48.8582 , 2.2945) 
     }else {
         retunr (0,0)
     }
}

But what if user has required another location? Then your function will return only (0,0). In this case you should have a solution for all 3 location and it's a good idea to use switch statement. And if you have learned about switch statements then your solution should look like that:

func coordinates(for location: String) -> (Double, 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)

         // But if user required an unknown location return a default value 
        default : return (0,0)
    }

}