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
Shawn Weiland
5,663 PointsXcode Update: Treehouse Code is broken and does not compile URL to forecast.io
I got the code working in playground with: let absoluteURL = forecastURL?.absoluteURL
But in xcode I get an error when I use it. Any help would be MUCH appreciated.
Here is my code for ForecastService function:
import Foundation
struct ForecastService {
let forecastAPIKey: String
let forecastBaseURL: NSURL?
init(APIKey: String) {
forecastAPIKey = APIKey
forecastBaseURL = NSURL(string: "https://api.forcast.io/forecast/\(forecastAPIKey)/")
}
func getForecast(lat: Double, long: Double, completion: (CurrentWeather? -> Void)) {
if let forecastURL = NSURL(string: "37.8267,-122.423/", relativeToURL: forecastBaseURL)
{
let networkOperation = NetworkOperation(url: forecastURL)
networkOperation.downloadJSONFromURL {
(let JSONDictionary) in
let currentWeather = self.currentWeatherFromJSON(JSONDictionary)
completion(currentWeather)
}
}
else {
print("Could not construct valid URL.")
}
}
func currentWeatherFromJSON(jsonDisctionary: [String: AnyObject]?) -> CurrentWeather?
{
if let currentWeatherDictionary = jsonDisctionary?["currently"] as? [String: AnyObject]{
return CurrentWeather(weatherDictionary: currentWeatherDictionary)
} else {
print("JSON Dictionary returned nil for 'currenty' key")
return nil
}
}
}