Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Weather App with Swift (Retired) Pulling Data From the Web Making a Network Call

Akhil Mohanlal
Akhil Mohanlal
2,128 Points

Weather data returning nil with NSData() call. However, the URL formed works.

let forecastBaseUrl = "https://api.forecast.io/forecast/\(forecastApiKey)/"

let finalLocation = locationPoints.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

if let forecastUrl = NSURL(string: "\(forecastBaseUrl)\(finalLocation)")

{

let weatherData = NSData(contentsOfURL: forecastUrl, options: nil, error: nil)

println(weatherData)

 }

forecastBaseUrl String "https://api.forecast.io/forecast/APIKEY/"

finalLocation String "37.8267,-122.423"

forecastUrl NSURL "https://api.forecast.io/forecast/APIKEY/37.8267,-122.423"

weatherData NSData? nil

The url pasted in the web browser works fine. What's wrong in the code, so that weatherData is returned nil ? Please help.

2 Answers

Ra’Shii Henry
Ra’Shii Henry
16,433 Points

i believe where you defined your constant forecastURL, you interpolated your strings incorrectly. The syntax consists of: backslash(type constant here)

you, however, have two before you insert each string. I haven't reviewed the swift weather app but just from looking above

I believe that's why when you print, it's returning nil because it's not even receiving the values from the first two constants.

let me know what happens.

Akhil Mohanlal
Akhil Mohanlal
2,128 Points

Hi Rashii

Thank you for pointing that out. When I copied my code to the forum I did not use 'markdown' feature. Hence I had to put a escape char before the slash, so that it prints the '\' once. But when I converted the snippet to markdown i forgot to take it out.

In summary, even with the slash removed the weatherData is nil. If you see the variable stack I pasted above the 'forecastUrl' is formed properly. If I paste that on a web browser, it works.

Anything else you see odd in the code ? Appreciate your help !!!

Happy Thanks Giving !!!

Regards Akhil

Akhil Mohanlal
Akhil Mohanlal
2,128 Points

I finally figured it out. The error I was getting said ' connection lost '. I was on my company VPN (Virtual Private Network). Once I disconnected the VPN, restarted the xCode and simulator, it worked for me... Thank you Rashii for taking time !!!

Ra’Shii Henry
Ra’Shii Henry
16,433 Points

Sorry i was unable to respond in time.

But it seems like the VPN interfered with you being able to fetch the data from the URL and print it in the console.

I actually just learned something too, so thank you as well.

glad you figured it out!