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
Pierre Smith
11,842 Pointswhy am I getting nil when unwrapping optional with my location manager?
My goal at this moment is to use the cllocationManager with the swift weather app. But everytime I try to use location services I get the error nil when unwrapping optional (the optional is the string variable that i'm using to store the coordinates of the location I would like to get the weather for. here's my code.
let locationManager = CLLocationManager()
// var coordinates: String = "43.855759,-79.086491" var coordinates: String?
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib.
aivRefresh.hidden = true
self.locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
// creates an instance of a location manager and
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
// println("(locationManager.location.coordinate.latitude),(locationManager.location.coordinate.longitude)")
// coordinates = "(locationManager.location.coordinate.latitude),(locationManager.location.coordinate.longitude)" // getCurrentWeatherData() println(coordinates) } else { // if there location services isn't
self.locationManager.requestAlwaysAuthorization()
}
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) { println("Error! Couldn't get location\nError: (error)") }
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){
var location:CLLocationCoordinate2D = manager.location.coordinate
println("\(manager.location)")
if locations != nil {
// coordinates = "(location.longitude),(location.latitude)" var coordinates = "43.835913,-79.086248" println(coordinates) } else {
coordinates = "43.835913,-79.086248"
println(coordinates)
}
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
if error == nil {
let pmarks = placemarks[0] as CLPlacemark
self.displayLocationInfo(pmarks)
} else {
println("Error: \(error.localizedDescription)")
}
})
}
// --------------------------------------------------------------------------- func displayLocationInfo(placemark: CLPlacemark) -> Void {
self.locationManager.stopUpdatingLocation()
self.lblCurrentLocation.text = "\(placemark.administrativeArea), \(placemark.locality)"
} // --------------------------------------------------------------------------- func getCurrentWeatherData() -> Void {
let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
let forecastURL = NSURL(string: coordinates!, relativeToURL: baseURL)
// session object that's a singleton (only one instance possible of this object). This ensures that there won't be other creations of a session instaince.
let sharedSession = NSURLSession.sharedSession()
//creates a task that temporarely writes the information to a location on disk
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
if error == nil {
//var urlContents = NSString(contentsOfURL: location, encoding: NSUTF8StringEncoding, error: nil) This pulls all data from the url given
let dataObject = NSData(contentsOfURL: location)
let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSDictionary
let currentWeather = Current(weatherDictionary: weatherDictionary)
// (currentWeather.temperature - 32) * 5/9 //Celcius conversion println(currentWeather.temperature) //test
dispatch_async(dispatch_get_main_queue(), { () -> Void in
//add self to make it specific since it's in a closure
self.iconView.image = currentWeather.icon!
self.lblCurrentTemperature.text = "\((currentWeather.temperature-32)*5/9)"
self.lblCurrentTime.text = "Last updated \(currentWeather.currentTime!)"
self.lblCurrentHumidity.text = "\(currentWeather.humidity)"
self.lblCurrentPercipitation.text = "\(currentWeather.precipitationProbability)"
self.lblSummary.text = "\(currentWeather.summary)"
//when done loading
self.aivRefresh.stopAnimating()
self.aivRefresh.hidden = true
self.btnRefresh.hidden = false
})
}else {
let networkIssueController = UIAlertController(title: "Error", message: "unable to load data", preferredStyle: .Alert)
let btnOk = UIAlertAction(title: "OK", style: .Default, handler: nil)
let btnCancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
networkIssueController.addAction(btnOk)
networkIssueController.addAction(btnCancel)
self.presentViewController(networkIssueController, animated: true, completion: nil)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.aivRefresh.stopAnimating()
self.btnRefresh.hidden = false
self.aivRefresh.hidden = true
})
}
})
downloadTask.resume()
} // --------------------------------------------------------------------------- @IBAction func refresh() {
locationManager.startUpdatingLocation()
getCurrentWeatherData()
btnRefresh.hidden = true
aivRefresh.hidden = false
// btnRefresh.enabled = false
aivRefresh.startAnimating()
}