ios - 我试图放下别针,但出现此错误 : Cannot assign value of type 'CLLocationManager' to type 'CLLocationCoordinate2D'

标签 ios swift

@IBAction func drop(sender: AnyObject)
{
     let mapAnnotation        = MKPointAnnotation()
     mapAnnotation.coordinate = locationManager   //  Cannot assign value of type 'CLLocationManager' to type 'CLLocationCoordinate2D'
     mapAnnotation.title      = "If you want a title"
     mapAnnotation.subtitle   = "or subtitle"
     myMap.addAnnotation(mapAnnotation)
}

最佳答案

如错误所述,您不能将 CLLocationManager 对象分配给 CLLocationCoordinate2D 类型的属性。

您需要将代码更改为:

mapAnnotation.coordinate = locationManager.location!.coordinate

由于各种原因,location 属性可能为nil,强制解包会导致崩溃。所以最好使用安全解包:

if let loc = locationManager.location
{
   mapAnnotation.coordinate = loc.coordinate
}

引用资料:

  1. MKPointAnnotation - coordinate
  2. CLLocationManager - location

关于ios - 我试图放下别针,但出现此错误 : Cannot assign value of type 'CLLocationManager' to type 'CLLocationCoordinate2D' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37622089/

相关文章:

ios - 注入(inject)类而不是实例时内存泄漏?

ios - 在 xib 上,拖动 View 会自动进入更大的 View

ios - 隐藏后的快速键盘高度

animation - 核心动画 : why can I not composite two animations?

ios - Google Sign In SDK实例化VC为零

ios - 如何为选定的 tableView 行创建自定义图像复选标记附件?

ios - 将按钮添加到 UIPickerView 以转到下一个数字

objective-c - 单击返回按钮时添加功能 -ios

json - 如何将字符串作为 json 转换为简单字符串 - SwiftyJSON

启用绘图的 iOS 四舍五入 UIView 角