我是iOS开发的新手,很抱歉,如果我不知道此问题有一个简单的解决方法。
因此,我当前正在创建一个需要用户当前位置作为地址的应用程序。
因此,在类标题中,我确保包含CLLLocationManagerDelegate:
class SignupViewController: UIViewController, CLLocationManagerDelegate {...}
接下来,我为位置管理器创建了一个实例变量:
let locationManager = CLLocationManager()
我还创建了CLLocationManagerDelegate函数:
// MARK: CLLocationManagerDelegate functions
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
self.getLocationAddress(location)
}
}
func getLocationAddress(location:CLLocation) -> CLPlacemark? {
let geocoder = CLGeocoder()
print("-> Finding user address...")
var placemark:CLPlacemark!
geocoder.reverseGeocodeLocation(location, completionHandler: {(placemarks, error)->Void in
if error == nil && placemarks!.count > 0 {
placemark = placemarks![0] as CLPlacemark
print(location)
if placemark != nil {
print(CLPlacemark().toString(placemark))
} else {
print("Problem with data received from geocoder")
}
}
})
return placemark
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error when updating location " + error.localizedDescription)
}
// MARK: Helper functions
func getLocation() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
我也有一个函数toString定义为CLPlacemark的扩展:
extension CLPlacemark {
func toString(placemark: CLPlacemark) -> String {
var addressString : String = ""
if placemark.ISOcountryCode == "TW" /*Address Format in Chinese*/ {
if placemark.country != nil {
addressString = placemark.country!
}
if placemark.subAdministrativeArea != nil {
addressString = addressString + placemark.subAdministrativeArea! + ", "
}
if placemark.postalCode != nil {
addressString = addressString + placemark.postalCode! + " "
}
if placemark.locality != nil {
addressString = addressString + placemark.locality!
}
if placemark.thoroughfare != nil {
addressString = addressString + placemark.thoroughfare!
}
if placemark.subThoroughfare != nil {
addressString = addressString + placemark.subThoroughfare!
}
} else {
if placemark.subThoroughfare != nil {
addressString = placemark.subThoroughfare! + " "
}
if placemark.thoroughfare != nil {
addressString = addressString + placemark.thoroughfare! + ", "
}
if placemark.postalCode != nil {
addressString = addressString + placemark.postalCode! + " "
}
if placemark.locality != nil {
addressString = addressString + placemark.locality! + ", "
}
if placemark.administrativeArea != nil {
addressString = addressString + placemark.administrativeArea! + " "
}
if placemark.country != nil {
addressString = addressString + placemark.country!
}
}
return addressString
}
当我运行我的代码时,最初似乎一切正常,这是控制台中的输出:
-> Finding user address...
-> Finding user address...
-> Finding user address...
-> Finding user address...
<+40.10886714,-88.23303354> +/- 10.00m (speed 0.00 mps / course 0.00) @ 5/8/16, 11:34:22 PM Central Daylight Time
401 E John St, 61820 Champaign, IL United States
(lldb)
但是,最后它崩溃并显示(lldb),并显示错误线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x8)
不太清楚为什么打印地址后应用程序崩溃了。
在此先感谢您的帮助!
但我不知道该怎么解决,
而不是调用扩展功能为
打印(CLPlacemark()。toString(placemark))
这样称呼它,
打印(placemark.toString(placemark))
因为存在内存泄漏问题。现在,它没有崩溃。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句