简体   繁体   English

尝试获取设备位置时 iOS 应用程序崩溃 - 线程 1:EXC_BAD_ACCESS

[英]iOS application crashes when trying to get device location - Thread 1: EXC_BAD_ACCESS

I am fairly new to iOS development, so sorry if this question has an easy fix of which I am unaware.我对 iOS 开发还很陌生,如果这个问题有一个我不知道的简单解决方法,我很抱歉。

So I am currently creating an application which requires the user's current location as an address.所以我目前正在创建一个应用程序,它需要用户的当前位置作为地址。

So in the class header I made sure to include CLLLocationManagerDelegate:所以在类头中我确保包含 CLLLocationManagerDelegate:

class SignupViewController: UIViewController, CLLocationManagerDelegate  {...}

Next I created an instance variable for the location manager:接下来我为位置管理器创建了一个实例变量:

let locationManager = CLLocationManager()

I also created the CLLocationManagerDelegate functions:我还创建了 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()
}

I also have a function toString defined as an extension to CLPlacemark:我还有一个函数 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
}

When I run my code everything seems to work fine initially, this is the output in the console:当我运行我的代码时,最初似乎一切正常,这是控制台中的输出:

-> 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)

However, at the end it crashes with an (lldb)and displays the error Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)但是,最后它与 (lldb) 崩溃并显示错误 Thread 1: EXC_BAD_ACCESS (code=1, address=0x8) 它还显示错误 Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)

控制台截图

Not too sure as to why the app is crashing after printing the address.不太确定为什么应用程序在打印地址后崩溃。

Thanks in advance for the help!在此先感谢您的帮助!

but i don't know which fix this,但我不知道哪个能解决这个问题,

Instead of calling the extension function as而不是调用扩展函数

print(CLPlacemark().toString(placemark))打印(CLPlacemark()。toString(地标))

call it as like this,像这样称呼它,

print(placemark.toString(placemark))打印(地标.toString(地标))

because there is a memory leakage problem.因为存在内存泄漏问题。 Now, Its not crashing.现在,它没有崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS:尝试获取音频帧值时为EXC_BAD_ACCESS - iOS: EXC_BAD_ACCESS when trying to get audioframe value 当使用EXC_BAD_ACCESS调用endUpdates时,UITableView在iOS 9上崩溃 - UITableView crashes on iOS 9 when endUpdates is called with EXC_BAD_ACCESS 使用iPhone设备进行测试时出现UIAlertController错误(线程1:EXC_BAD_ACCESS)iOs SWIFT - UIAlertController error when testing using iPhone device (Thread 1: EXC_BAD_ACCESS) iOs SWIFT presentViewController在iOS 8上因EXC_BAD_ACCESS崩溃 - presentViewController crashes with EXC_BAD_ACCESS on iOS 8 iOS因EXC_BAD_ACCESS(代码= 1)崩溃 - iOS crashes with EXC_BAD_ACCESS(code=1) iOS应用因EXC_BAD_ACCESS崩溃 - iOS app crashes with EXC_BAD_ACCESS 尝试获取UISegmentedControl的selectedSegmentIndex时的EXC_BAD_ACCESS - EXC_BAD_ACCESS when trying to get selectedSegmentIndex of UISegmentedControl UICollectionView崩溃,并显示错误:线程1:EXC_BAD_ACCESS - UICollectionView Crashes with Error : Thread 1: EXC_BAD_ACCESS 在iOS ARC中,我的递归函数使用EXC_BAD_ACCESS使应用程序崩溃 - In iOS ARC my recursive function crashes application with EXC_BAD_ACCESS 返回上一个视图时,应用程序崩溃-线程1:EXC_BAD_ACCESS - Aplication crashes when returning to previous View - Thread 1: EXC_BAD_ACCESS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM