gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 12:47:18 25 4
gpt4 key购买 nike

@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/

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