I am fairly new to iOS development, so sorry if this question has an easy fix of which I am unaware.
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:
class SignupViewController: UIViewController, CLLocationManagerDelegate {...}
Next I created an instance variable for the location manager:
let locationManager = CLLocationManager()
I also created the CLLocationManagerDelegate functions:
// 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:
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)
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))
call it as like this,
print(placemark.toString(placemark))
because there is a memory leakage problem. Now, Its not crashing.