iOS应用程序在尝试获取设备位置时崩溃-线程1:EXC\u BAD\u访问
我对iOS开发相当陌生,所以如果这个问题有一个我不知道的简单的解决方法,那么很抱歉 因此,我目前正在创建一个应用程序,它需要用户的当前位置作为地址 因此,在类标题中,我确保包含CLLLocationManagerDelegate:iOS应用程序在尝试获取设备位置时崩溃-线程1:EXC\u BAD\u访问,ios,swift,cllocationmanager,exc-bad-access,clplacemark,Ios,Swift,Cllocationmanager,Exc Bad Access,Clplacemark,我对iOS开发相当陌生,所以如果这个问题有一个我不知道的简单的解决方法,那么很抱歉 因此,我目前正在创建一个应用程序,它需要用户的当前位置作为地址 因此,在类标题中,我确保包含CLLLocationManagerDelegate: class SignupViewController: UIViewController, CLLocationManagerDelegate {...} 接下来,我为location manager创建了一个实例变量: let locationManager =
class SignupViewController: UIViewController, CLLocationManagerDelegate {...}
接下来,我为location manager创建了一个实例变量:
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)
->正在查找用户地址。。。
->正在查找用户地址。。。
->正在查找用户地址。。。
->正在查找用户地址。。。
+/-2016年5月8日晚上11:34:22中央夏令时10.00米(速度0.00英里/航向0.00)
美国伊利诺伊州香槟市东约翰街401号,邮编61820
(lldb)
但是,最后它会与一个(lldb)崩溃,并显示错误线程1:EXC_BAD_ACCESS(code=1,address=0x8)
不太清楚为什么打印地址后应用程序会崩溃
提前感谢您的帮助 但我不知道是哪一个解决了这个问题 而不是调用扩展函数作为 打印(CLPlacemark().toString(placemark)) 就这样说吧, 打印(placemark.toString(placemark))
因为存在内存泄漏问题。现在,它没有崩溃。您介意发布控制台中出现的错误吗?很可能是
nil
,您试图访问它。控制台中没有显示其他内容。我在帖子中添加了一个屏幕截图。尝试根据帖子启用NSzombie,这可能会导致一些错误,可能会有所帮助。我启用了Zombie对象,但控制台仍然输出相同的错误:nothing but(lldb).你能在闭包的开始处放置一个断点,然后一行一行地遍历,看看它在哪里崩溃吗?无论如何,你为什么要调用toString
print()
应该调用placemarks
的description
属性。@ILikeTau实际上提问者的意图是将带有某些属性的placemarks值转换为地址字符串,因此只有他创建了一个扩展名并打印了该值