Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKPolygon using Swift (Missing argument for parameter 'interiorPolygons' in call)

Fellow Devs, I'm trying to implement a polygon overlay on a mapview as follows:

private func drawOverlayForObject(object: MyStruct) {
    if let coordinates: [CLLocationCoordinate2D] = object.geometry?.coordinates {
        let polygon = MKPolygon(coordinates: coordinates, count: coordinates.count)
        self.mapView.addOverlay(polygon)
    }
}

The following error is presented:

Missing argument for parameter 'interiorPolygons' in call

According to the documentation: Apple Docu:

Mutable Pointers

When a function is declared as taking an UnsafeMutablePointer argument, it can accept any of the following:

  • nil, which is passed as a null pointer
  • An UnsafeMutablePointer value
  • An in-out expression whose operand is a stored lvalue of type Type, which is passed as the address of the lvalue
  • An in-out [Type] value, which is passed as a pointer to the start of the array, and lifetime-extended for the duration of the call

Now I think that my approach then would be correct, providing a [CLLocationCoordinate2D] array. Did anyone experience the same problem and found a workaround?

thanks Ronny

like image 454
Ron Avatar asked Jan 10 '23 17:01

Ron


1 Answers

The error you're getting is Swift's cryptic way of saying that it can't find a method which matches your parameters. If you did try passing the interiorPolygons parameter, you'd get an equally confusing:

Extra argument 'interiorPolygons' in call

Your code is pretty close though; you just need a couple of minor changes. In the doc you reference, it says one of the things you can pass is:

An in-out [Type] value, which is passed as a pointer to the start of the array, and lifetime-extended for the duration of the call

So, it's looking for an in-out parameter. Which is done by passing coordinates prefixed with an &, like so:

MKPolygon(coordinates: &coordinates, count: coordinates.count)

But, in-out parameters can't be constants. From the docs:

You can only pass a variable as the argument for an in-out parameter. You cannot pass a constant or a literal value as the argument, because constants and literals cannot be modified.

So, you need to define coordinates with a var first:

if var coordinates: [CLLocationCoordinate2D] = object.geometry?.coordinates

Which makes the entire function look like this:

private func drawOverlayForObject(object: MyStruct) {
    if var coordinates: [CLLocationCoordinate2D] = object.geometry?.coordinates {
        let polygon = MKPolygon(coordinates: &coordinates, count: coordinates.count)
        self.mapView.addOverlay(polygon)
    }
}
like image 90
Mike S Avatar answered Jan 16 '23 21:01

Mike S