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
Matt Cantrill
5,685 Pointsxcode 6.1 error: trying to unwrap an optional but returns nil.
i am fairly new to swift but this is a problem that i can't find the answer to. every time i try to run my app to make a network call it pauses my app and complains that it found nil in an optional it was trying to unwrap.
here is my code:
import UIKit
class ViewController: UIViewController {
private let apiKey = "ca73b3ab341efe41e970ae3f86270cc3"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let forecastBaseURL = NSURL(string: "https:api.forecast.io/forecast/\(apiKey)/")
var forecastURL = NSURL(string: "-37.839337, 147.619493", relativeToURL: forecastBaseURL)
let weatherData = NSData(contentsOfURL: forecastURL!)
println(weatherData)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
1 Answer
Chris Shaw
26,676 PointsHi Matt,
This error is a bit tricky because of the error returned, I got thrown by it when I first went through the course but after looking carefully noticed an space after the comma in the coordinates which was causing the error, simply remove that and the error will simply vanish as if nothing went wrong.
var forecastURL = NSURL(string: "-37.839337,147.619493", relativeToURL: forecastBaseURL)
Happy coding!
Matt Cantrill
5,685 PointsMatt Cantrill
5,685 PointsThank you so much. I never would have figured that out.