gpt4 book ai didi

ios7 - 使用 Swift 的 MKPolygon(调用中参数 'interiorPolygons' 缺少参数)

转载 作者:搜寻专家 更新时间:2023-10-31 08:17:05 24 4
gpt4 key购买 nike

各位开发者,我正在尝试在 mapview 上实现多边形叠加层,如下所示:

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

出现以下错误:

Missing argument for parameter 'interiorPolygons' in call

根据文档: Apple Docu:

Mutable Pointers

When a function is declared as taking an UnsafeMutablePointerargument, 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

现在我认为我的方法是正确的,提供一个 [CLLocationCoordinate2D] 数组。有没有人遇到同样的问题并找到解决方法?

谢谢罗尼

最佳答案

您收到的错误是 Swift 的隐晦方式,表示它找不到与您的参数匹配的方法。如果您确实尝试传递 interiorPolygons 参数,您会得到同样令人困惑的信息:

Extra argument 'interiorPolygons' in call

虽然您的代码非常接近;你只需要做一些小的改动。在您引用的文档中,它说您可以传递的其中一件事是:

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

因此,它正在寻找 in-out parameter .这是通过传递以 & 为前缀的 coordinates 来完成的,如下所示:

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

但是,输入输出参数不能是常量。来自文档:

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.

因此,您需要先使用 var 定义 coordinates:

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

这使得整个函数看起来像这样:

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

关于ios7 - 使用 Swift 的 MKPolygon(调用中参数 'interiorPolygons' 缺少参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26007584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com