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
Tim Edgren
7,437 Pointsgot the “unexpectedly found nil while unwrapping an Optional value” error
m using google places api to and use the coordinates to put an annotation on an apple maps.
What I want to do now is to translate coordinates to name and address and use it for the Ekreminder. This is my code so far but i get the error "fatal error: unexpectedly found nil while unwrapping an Optional value" when i try to run it:
addLocationReminderViewController.name = self.mapItemData.placemark.name
// Construct the address
let dic = self.mapItemData.placemark.addressDictionary as Dictionary!
let city = dic["City"] as! String
let state = dic["State"] as! String
let country = dic["Country"] as! String
addLocationReminderViewController.address = "\(city), \(state), \(country)"
}
3 Answers
Tim Edgren
7,437 PointsThis is producing nil:
if data != nil{
let dic = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableLeaves) as! NSDictionary
let lat = dic["results"]?.valueForKey("geometry")?.valueForKey("location")?.valueForKey("lat")?.objectAtIndex(0) as! Double
let lon = dic["results"]?.valueForKey("geometry")?.valueForKey("location")?.valueForKey("lng")?.objectAtIndex(0) as! Double
let point = MKPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
point.title = self.resultsArray[indexPath.row]
let region = MKCoordinateRegion(center: point.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.mapView.setRegion(region, animated: true)
self.mapView.addAnnotation(point)
self.mapView.selectAnnotation(point, animated: true)
//
let placemark = MKPlacemark(coordinate: point.coordinate, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
self.mapItemData = mapItem
//
})
Steve Hunter
57,712 PointsThere must be a specific line that's unwrapping an optional to find nil rather than the expected value? Is it the test on the first line, such that data is nil, meaning the rest of that code doesn't run - or is it some other line?
Steve.
Tim Edgren
7,437 PointsIm trying to translate it to get the address to show in a string
Tim Edgren
7,437 PointsThis maybe:
override func viewDidLoad() {
super.viewDidLoad()
// Display the name and address of the location associated with this new reminder
self.nameLabel.text = "Location: \(self.name ?? "")"
self.addressName.text = self.name
self.addressName.text = self.address
}
Steve Hunter
57,712 PointsOK - you're going to have to pin the error down far more accurately. There's no way we can look at snippets of your code with no context as to what files the code is in within your project and come up with a solution to such a broad error.
Use some breakpoints, explore what works and what doesn't and generate some context for your question. For example, is your data variable nil? If it isn't what's in there? What's contained in your dic dictionary? Does it hold a dictionary, or nothing?
You could also push your project to a Github-type resource so we can pull your code down and replicate the error.
But if you can't say which bit of code is causing the error it is impossible for me to second-guess what the problem might be.
Sorry to be unhelpful, but there's nothing for me to go on here.
Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsWhat produces
nil? Is it the dictionary itself, or one of its fields?Steve.