2017-04-01 235 views
0

我在我的info.plst文件中包含了NSLocationWhenInUsageDescriptionNSLocationAlwaysUsageDescription,我的代碼是這樣的。locationManager委託不工作

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?」

你能告訴我問題在哪裏嗎?

回答

1

不能分配的類型 「LocationSelectorViewController」 價值型 「CLLocationManagerDelegate?」

答: 你LocationSelectorViewController類必須採納和遵守CLLocationManagerDelegate協議。

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

class LocationSelectorViewController: UIViewController, 
UISearchBarDelegate, MKMapViewDelegate, CLLocationManagerDelegate { 

,而不是

class LocationSelectorViewController: UIViewController, 
UISearchBarDelegate, MKMapViewDelegate {