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 Build a Weather App with Swift Interacting with Data From the Web JavaScript Object Notation

Mateo Sossah
Mateo Sossah
6,598 Points

Error when creating the URL in the Playground

The following code gives me a result in the playground instead of a correct URL (37.8267,-122.423 --https://api.forecast.io/forecast/a4b5ad81ec60b94c6c6a4e338ae68978/) If someone has had the same issue or know why, it'd be great to get some help! Thanks in advance

import Foundation

let forecastAPIKey = "a4b5ad81ec60b94c6c6a4e338ae68978"
let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(forecastAPIKey)/")
let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL)

4 Answers

Duncan Hall
Duncan Hall
6,417 Points

Looking at the documentation for NSURL (https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/), there's a hint that there's one more step.

import Foundation

let forecastAPIKey = "abcdefghijklmnopqrstuvwxyz123456" let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(forecastAPIKey)/") let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL) // THIS IS THE NEW BIT // let absoluteURL = forecastURL?.absoluteURL

I am unsure at this time whether this is needed in the app itself, but it produces the correct sidebar output in a playground. I guess NSURL relativeToURL keeps both components semi-separated in the newest version of Xcode?

Lucy Jeong
Lucy Jeong
7,243 Points

Hi, I got same problem. you could change a sequence, https://api.forecast.io/forecast/a4b5ad81ec60b94c6c6a4e338ae68978/37.8267,-122.423 and then you can access

Christian Dangaard
Christian Dangaard
3,378 Points

Using the following information: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/

I added an extra line (if someone can correct me at this point feel free to do so please)

let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(forecastAPIKey)/")

let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL)

let absoluteForecastURL = forecastURL?.absoluteURL

I've automatically thought perhaps we could simply use the APIKey as part of the base URL so I could just use forecastURL instead of absoluteForecastURL.

Schott Taylor
PLUS
Schott Taylor
Courses Plus Student 7,125 Points

another solution to this instead of let absoluteForecastURL = forecastURL?.absoluteURL,

when you write out your letforecastURL statement, the menu on the right gives you: https://api.forecast.io/forecast/a4b5ad81ec60b94c6c6a4e338ae68978/

all you have to do is copy and paste the coordinates onto the end of the URL, all that absoluteForecastURL does for you is adds 1+1 together.