我在info.plst文件中包含了NSLocationWhenInUsageDescription
和NSLocationAlwaysUsageDescription
,代码如下。
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) {
}
}
我想我没有错,但它一直在显示这个错误
无法将“LocationSelectorViewController”类型的值分配给“CLLocationManagerDelegate?”
你能告诉我哪里有问题吗?
最佳答案
无法将“LocationSelectorViewController”类型的值分配给“CLLocationManagerDelegate?”
答复:
您的LocationSelectorViewController
类必须采用并符合CLLocationManagerDelegate
协议。
在类中添加CLLocationManagerDelegate
,如下所示。
class LocationSelectorViewController: UIViewController,
UISearchBarDelegate, MKMapViewDelegate, CLLocationManagerDelegate {
而不是
class LocationSelectorViewController: UIViewController,
UISearchBarDelegate, MKMapViewDelegate {