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

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

我是 iOS 开发的新手,如果这个问题有一个我不知道的简单解决方案,我很抱歉。

所以我目前正在创建一个需要用户当前位置作为地址的应用程序。

所以在 class header 中我确保包含 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) 崩溃并显示错误 Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)

不太确定打印地址后应用崩溃的原因。

在此先感谢您的帮助!

但我不知道哪个解决了这个问题,

而不是调用扩展函数

print(CLPlacemark().toString(placemark))

这样称呼它,

print(placemark.toString(placemark))

因为存在内存泄漏问题。现在,它没有崩溃。