[Chapter 10] Location - Cannot convert value of type '(Int, Int)' to expected argument type 'Location'

In chapter 10, under “Introducing Methods”, I’m getting this error:

Cannot convert value of type '(Int, Int)' to expected argument type 'Location'

func isInDeliveryRange(location:)
let areas = [
  DeliveryArea(center: Location(x: 2, y: 4), radius: 2.5),
  DeliveryArea(center: Location(x: 9, y: 7), radius: 4.5)
]

func isInDeliveryRange(_ location: Location) -> Bool {
  for area in areas {
    let distanceToStore = 
      distance(from: (area.center.x, area.center.y),
                 to: (location.x, location.y))

    if distanceToStore < area.radius {
      return true
    }
  }

When I option-click area.center it confirms that my ‘areas’ centers are defined as Location.

'areas' constant
let areas = [
     DeliveryArea(center: Location(x: 2, y: 4), radius: 2.5), 
     DeliveryArea(center: Location(x: 9, y: 7), radius: 4.5)
]
// area[0].center defines as a Location. 

When I change distanceToStore distance as locations like this, it runs as expected.

let distanceToStore = distance(from: Location(x: area.center.x, y: area.center.y), to: Location(x: location.x, y: location.y))

I can make it work, but it would be convenient if I could get these coordinates to define implicitly as Location. What am I doing wrong?

So the distance() function is defined something like this:

func distance(from: Location, to: Location) -> Float {
     //do stuff
}

And, the Location class is defined something like this:

class Location {
    var x: Int
    var y: Int

    init(x: Int, y: Int) {
        self.x = x
        self.y = y
    }
}

And area.center is a Location object, which means area.center.x is an Integer and area.center.y is an Integer.

When I option-click area.center it confirms that my ‘areas’ centers are defined as Location.

Then all you have to do is:

distance(from: area.center, to: location)

Well, geez. That definitely cleans up the code! And now that I’m looking at it, should have been an obvious solution.

BUT, my original post shows verbatim what was in the book. I can see logically why the code in the book would consider those coordinates to be type (x,y) and not Location — BUT … for the sake of understanding … why/how is the code in my original post shown in the book as instruction? In other words, why can’t I match what is in the book?

Thanks!

Post the definition of the distance() function in the book, and repost exactly what code you are referring to that is in the book so that I know what you are talking about.

I am using the epub version, so page numbers would not help, but it’s this snippet, the distanceToStore constant:

let areas = [
  DeliveryArea(center: Location(x: 2, y: 4), radius: 2.5),
  DeliveryArea(center: Location(x: 9, y: 7), radius: 4.5)
]

func isInDeliveryRange(_ location: Location) -> Bool {
  for area in areas {
let distanceToStore = 
  distance(from: (area.center.x, area.center.y),
             to: (location.x, location.y))

if distanceToStore < area.radius {
  return true
}
  }

Thanks!

The first version of the distance func looked like this (it’s at the very beginning of the chapter):

func distance(from source: (x: Int, y: Int), to target: (x: Int, y: Int)) 
-> Double 
{....

To call that, you have to spell out each int separately, as two tuples of two ints each. The earlier code calls it this way.

Further down (six pages or so), there is a mini-exercise described thus:
• Change distance(from:to:) to use Locations as parameters instead of x-y tuples.

Once you do that (I think you did!), you now have to pass in Locations, and can’t just pass tuples.

So when you get to the point where this call works:

 let distanceToStore = 
      distance(from: Location(x: area.center.x, y: area.center.y), 
                 to: Location(x: location.x, y: location.y))

you are passing in locations, and can just simplify it to:

let distanceToStore = distance(from: area.center, to: location)

This topic was automatically closed after 166 days. New replies are no longer allowed.