ios - locationManager 代表不工作

标签 ios swift swift3 mapkit

我在我的 info.plst 文件中包含了 NSLocationWhenInUsageDescriptionNSLocationAlwaysUsageDescription,我的代码是这样的。

import UIKit
import MapKit



class LocationSelectorViewController: UIViewController, 
UISearchBarDelegate, MKMapViewDelegate {

@IBOutlet weak var locationSearchBar: UISearchBar!
@IBOutlet weak var mapView: MKMapView!
var selectedLoc: String?
let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.requestAlwaysAuthorization()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.startUpdatingLocation()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func locationManager(manager: CLLocationManager, didupdateLocations locations: [CLLocation]) {

}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {

}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

}
}

我认为我没有错,但它一直显示这个错误

Cannot Assign value of type "LocationSelectorViewController" to type "CLLocationManagerDelegate?"

你能告诉我问题出在哪里吗?

最佳答案

Cannot Assign value of type "LocationSelectorViewController" to type "CLLocationManagerDelegate?"

答: 您的 LocationSelectorViewController 类必须采用并符合CLLocationManagerDelegate 协议(protocol)。

在您的类中添加 CLLocationManagerDelegate,如下所示。

class LocationSelectorViewController: UIViewController, 
UISearchBarDelegate, MKMapViewDelegate, CLLocationManagerDelegate {

代替

 class LocationSelectorViewController: UIViewController, 
UISearchBarDelegate, MKMapViewDelegate {

关于ios - locationManager 代表不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43158223/

相关文章:

iOS Swift - UITableView 的最佳方法

ios - 将 Travis CI 与 Swift 3.0 和 CocoaPods 结合使用

ios - 查看卡住与多个 DispatchQueue.main.async 使用

ios - Swift DateFormatter 可选毫秒数

ios - 在 didSet 中调用函数在类中不起作用

iphone - 使用 ARC 时声明委托(delegate)

ios - DispatchQueue 同步并发

ios - 强烈提及委托(delegate)

ios - 无法使用 UITableViewDelegate

ios - 展平具有重复值的字典数组 Swift 3.0