我在我的 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) {
}
}
我认为我没有错,但它一直显示这个错误
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/