Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Pierre Smith
Pierre Smith
11,842 Points

Swift: Trying to create a locationManager class to handle location data then call it in my root view controller.

I was attempting to make a class that handles location services and call in my root view controller however in doing so My app will not load, I get a indefinite black screen w/ the exception: thread 1:EXC_BAD_ACCESS (code=2, address=0x7ff...). It seems to be accusing because in order to change objects belonging to my root view controller I created a instance of the controller. Is there a way to retrieve the label objects belonging to my root view controller without creating an instance? How do I fix this?

here's my code for the RootViewController:

import UIKit


class RootViewController: UIViewController {


@IBOutlet weak var lblCurrentLocation: UILabel!


let locationTracker = LocationTracker()

 override func viewDidLoad() {
   super.viewDidLoad()
   // Do any additional setup after loading the view, typically from a nib.

  locationTracker.startTracking()
}

and my locationManager class:

import UIKit import Foundation import CoreLocation

class LocationTracker: NSObject, CLLocationManagerDelegate {

let locationManager = CLLocationManager()
let rootViewController = RootViewController()


func startTracking() {
  locationManager.delegate = self
  locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters

  locationManager.requestWhenInUseAuthorization()
  locationManager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

  var currentLocation: CLLocation = manager.location

  var coordinates: String = "\(currentLocation.coordinate.latitude),\(currentLocation.coordinate.longitude)"


  println(" Locations data: \(locations)")

  getLocationPlacemark(currentLocation: currentLocation)

}


 func displayLocationInfo  (#Placemark: CLPlacemark) {

locationManager.stopUpdatingLocation()

self.rootViewController.lblCurrentLocation.text = "\(Placemark.administrativeArea), \(Placemark.locality)"
}


func getLocationPlacemark (#currentLocation:CLLocation)-> Void {

  CLGeocoder().reverseGeocodeLocation(currentLocation, completionHandler: { (placemark, error) -> Void in

  if error == nil && placemark.count > 0 {

    var pMark = placemark [0] as CLPlacemark

    dispatch_async(dispatch_get_main_queue(), { () -> Void in

      self.displayLocationInfo(Placemark: pMark)
    })


    }else {

      self.rootViewController.lblCurrentLocation.text = "error!"
    }
  })
}