我是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)
在打印地址后,应用程序崩溃的原因不太清楚。
提前感谢您的帮助!
答案 0 :(得分:1)
但我不知道是哪个修复,
而不是将扩展函数称为
打印(CLPlacemark()。的toString(地标))
这样称呼它,
打印(placemark.toString(地标))
因为存在内存泄漏问题。现在,它没有崩溃。