Swift Apprentice 2.0 Chapter 11 Structures - Introducing Methods

Hi,
I’m having trouble finding my error in the let distanceToStore statement: area.center.x to: area.center.y, throws an error: cannot convert value type ‘(Int, to: Int)’ to expected argument type ‘Location’. As far as I can tell I have the structs Location and Delivery identical as shown in the book and have an import Foundations statement at the top of the playground as well.
Thanks in advance. GK

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

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

if distanceToStore < area.range {
    return true
}

}
return false
}
let customerLocation1 = Location(x: 8, y: 1)
let customerLocation2 = Location(x: 5, y: 5)

What does your struct Location look like?

struct Location {
  let x: Int
  let y: Int
}

I’m still learning, so take my explanation with a grain of salt. I think that the error message is accurate. You are passing an (Int, Int) to a function which is expecting a Location. If you change the code to the following, this works:

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

Here, you are creating a new Location to pass into the function, which makes it happy. You initialize the Location with the x and y coords of interest.

If this is correct, I think this should be in the errata.

I also noticed the same issue on page 168:

let distanceFromCenter = distance(from: (center.x, center.y), to: (location.x, location.y))

should instead be

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

The distance(from: to:) function expects an (Int, Int) tuple as its one and only parameter and argument, not a Location one instead, so the original code should actually work as expected in this case after all for sure and for good indeed. Please go ahead and check out the chapter’s playground just to see how it’s all actually done in there and let me know if you have any more questions or issues regarding the whole thing afterwards.