ios - polylineWithCoordinates 烦人的参数错误

标签 ios swift mkpolyline

我正在尝试从名为 coordarray 的 2 元素 CLLocationCoordinate2D 数组和数字 2 中初始化折线,使用此代码:

self.line = MKPolyline.polylineWithCoordinates(coordarray,2)

但是我收到一个错误提示

Cannot invoke 'polylineWithCoordinates' with an argument list of type ([CLLocationCoordinate2D],Int)

我检查了文档,似乎我传递了正确的参数列表,我错过了什么?

最佳答案

该错误消息非常具有误导性 - 请检查 MKPolyline 的文档对于 Swift,实际上没有名为 polylineWithCoordinates 的静态方法,您可能正在查看文档的 Objective-C 版本*。也许你打算使用:

convenience init!(coordinates coords: UnsafeMutablePointer<CLLocationCoordinate2D>,
                  count count: Int)

在这种情况下,我认为您的代码需要:

// Note - coordarray is declared as var
var coordarray = // ...

self.line = MKPolyline(coordinates: &coordarray, count: 2)

这里有一个链接,详细介绍了如何添加 MKPolyline 覆盖:http://mobiletoolworks.com/adding-a-mkpolyline-overlay-using-swift-to-an-ios-mapkit-map/

*要将显示 Objective-C 的文档更改为 Swift,页面右上角有一个控件:

enter image description here

关于ios - polylineWithCoordinates 烦人的参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30291776/

相关文章:

swift - Alamofire 照片上传,正文中包含文件 key

ios - 如何使用 swift 将 MKPolyline 属性存储为可转换的 IOS 核心数据?

ios - 将类型作为变量传递

IOS Swift 读取 PCM Buffer

ios - WebRTC 中的调用组视频

swift - 从节中移动最后一行时如何删除节标题

ios - MapKit - 更改 MKPolyline 的颜色/描边

ios - MKMapView 中的叠加 MKPolyLine 问题(rendererForOverLay)

ios - 当 dataSource 数组正确时 UICollectionView 出现 "Index Out Of Range"错误

ios - UILabel sizeToFit 拆分数字