【问题标题】:Instance method 'mapView(_:didFailToLocateUserWithError:)' nearly matches optional requirement实例方法 'mapView(_:didFailToLocateUserWithError:)' 几乎符合可选要求
【发布时间】:2018-06-27 15:22:27
【问题描述】:

我在我的应用程序中使用MKMapView,它可以正常工作。我可以通过 didUpdate userLocation 方法正确获取更新后的位置。

我的问题是这样的:

实例方法 'mapView(_:didFailToLocateUserWithError:)' 几乎匹配协议 'MKMapViewDelegate' 的可选要求 'mapView(_:didFailToLocateUserWithError:)'

注意事项

  • 我写的方法完全如下:

    func mapView(_ mapView: MKMapView, didFailToLocateUserWithError error: Error) {
        //code
    }
    
  • 我已经实现了正确的协议:MKMapViewDelegate

  • didUpdateUserLocation 方法运行正常。

  • 我使用的是 Xcode 9.2,部署目标:10.0,Swift 3.2。

  • 我在方法签名处添加了@nonobjc,警告没有出现,但该方法从未执行。

【问题讨论】:

  • 故障可能不会发生
  • 相反,如果实现了所有 MKMapViewDelegate 方法都可以,除了我强烈需要的这个

标签: ios swift delegates mapkit


【解决方案1】:

确保添加了这个

 mapView.showsUserLocation = true

并检查授权

let locationManager = CLLocationManager()
if CLLocationManager.authorizationStatus() == .notDetermined {
  locationManager.requestWhenInUseAuthorization()
} else if CLLocationManager.authorizationStatus() ==   .authorizedWhenInUse {
   mapView.showsUserLocation = true
} else {
   //show alert
}

【讨论】:

  • 是的,我做到了,实际上方法didUpdate userLocation被正确访问了。
猜你喜欢
  • 2017-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
  • 2018-06-23
相关资源
最近更新 更多