原因是’委托必须响应locationManager:dIDUpdateLocations:’
这很奇怪,因为我的代码中有一个dIDUpdateLocations:函数.
我不知道发生了什么,任何帮助表示赞赏.我的VIEwController代码如下,谢谢!
/** copyright (c) 2016 Ahad Sheriff**/import UIKitimport Firebaseimport CoreLocationclass LoginVIEwController: UIVIEwController { // MARK: PropertIEs var ref: FIRDatabaseReference! // 1 var userID: String = "" var locationManager = CLLocationManager() overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() ref = FIRDatabase.database().reference() // 2 } @IBAction func loginDIDtouch(sender: AnyObject) { //ask user to enable location services locationManager.requestWhenInUseAuthorization() if CLLocationManager.locationServicesEnabled() { //collect user's location locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers locationManager.requestLocation() locationManager.startUpdatingLocation() let location = self.locationManager.location var latitude: Double = location!.coordinate.latitude var longitude: Double = location!.coordinate.longitude print("current latitude :: \(latitude)") print("current longitude :: \(longitude)") //Log in to application anonymously using Firebase FIRAuth.auth()?.signInAnonymouslyWithCompletion() { (user,error) in if let user = user { print("User is signed in with uID: ",user.uID) self.userID = user.uID } else { print("No user is signed in.") } self.performSegueWithIDentifIEr("Logintochat",sender: nil) } } else { print("Unable to determine location") } } overrIDe func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) { super.prepareForSegue(segue,sender: sender) let navVc = segue.destinationVIEwController as! UINavigationController // 1 let chatVc = navVc.vIEwControllers.first as! ChatVIEwController // 2 chatVc.senderID = userID // 3 chatVc.senderdisplayname = "" // 4 } func locationManager(manager: CLLocationManager!,dIDUpdateLocations locations: [AnyObject]!) { //--- CLGeocode to get address of current location ---// CLGeocoder().reverseGeocodeLocation(manager.location!,completionHandler: {(placemarks,error)->VoID in if (error != nil) { print("Reverse geocoder Failed with error" + error!.localizedDescription) return } if placemarks!.count > 0 { let pm = placemarks![0] as CLPlacemark self.displayLocationInfo(pm) } else { print("Problem with the data received from geocoder") } }) } func displayLocationInfo(placemark: CLPlacemark?) { if let containsPlacemark = placemark { //stop updating location locationManager.stopUpdatingLocation() let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : "" let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : "" let administrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : "" let country = (containsPlacemark.country != nil) ? containsPlacemark.country : "" print(locality) print(postalCode) print(administrativeArea) print(country) } } func locationManager(manager: CLLocationManager!,dIDFailWithError error: NSError!) { print("Error while updating location " + error.localizedDescription) }}解决方法 首先,明确添加您确认CLLocationManagerDelegate:
class LoginVIEwController: UIVIEwController,CLLocationManagerDelegate
其次,为CLLocationManager设置委托属性:
overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() locationManager.delegate = self ref = FIRDatabase.database().reference()}
第三,在CLLocationManagerDelegate doc中,我看到了与dIDUpdateLocations和dIDFailWithError方法的声明不同:
func locationManager(_ manager: CLLocationManager,dIDUpdateLocations locations: [CLLocation])func locationManager(_ manager: CLLocationManager,dIDFailWithError error: NSError)
此外,您在代码中几乎没有其他问题.它们都是关于不安全的展开.你不应该使用不安全的展开!到处.这样做是为了杀死选择哲学.另一方面,做正确的事情可以大大提高应用的稳定性.在loginDIDtouch函数中进行以下更正:
var latitude: Double? = location?.coordinate.latitudevar longitude: Double? = location?.coordinate.longitude
当您第一次调用此函数时,您的位置尚未确定(将以异步方式确定,您将获得委托方法中的位置),这就是使用force unwrapp时出现致命错误的原因.在dIDUpdateLocations函数中:
if let pm = placemarks?.first{ self.displayLocationInfo(pm) }
这部分代码的更正比原始代码短,并且可以防止您同时发生两次可能的崩溃 – 当地标为nil且地标不是nil时,则为空数组
总结以上是内存溢出为你收集整理的swift – 错误:’Delegate必须响应locationManager:didUpdateLocations:’收集用户位置时全部内容,希望文章能够帮你解决swift – 错误:’Delegate必须响应locationManager:didUpdateLocations:’收集用户位置时所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)