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 (Retired) Pulling Data From the Web JavaScript Object Notation

Oscar Alencar
Oscar Alencar
2,798 Points

NSURL returning an optional (some)

Hi, my NSURL is returning a Optional. This is different from the playground of the video. I can't access the url without unwrap it. Why is this happening?

4 Answers

Dominic Bryan
Dominic Bryan
14,452 Points

Hi Oscar,

If you look in the teacher notes on that video it says the following about xcode being updated and that NSURL in the updated version of xcode returns a optional value. this is simply changed by adding a bang (!) to your forecast constant.

also bare in mind .dataWithContentsOfURL() is no longer used, below I have pasted the teacher notes to show you what you must do.

Changes to Xcode

This course uses Xcode 6.0.1 and since its release, Xcode has been updated to Xcode 6.1. In the process some changes have been made to the APIs we work with in this course and in this particular video, the use of the NSData class initializer has been modified from

NSData.dataWithContentsOfURL(url: NSURL, options: NSDataReadingOptions, error: NSErrorPointer)

to

NSData(contentsOfURL: NSURL, options: NSDataReadingOptions, error: NSErrorPointer)

Additionally, the NSURL constructor now returns an optional so when you pass the url to the NSData method, it needs to be unwrapped.

In the video, where the code reads

let weatherData = NSData.dataWithContentsOfURL(forecastURL, options: nil, error: nil)

replace with

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

Hope this helped!

Dom

Solution that I found

// Replace 
let weatherData = NSData(contentsOfURL: forecastURL!) 

// width 
let weatherData = NSString(contentsOfURL: forecastURL!, encoding: 0, error: nil) 

Reference Apple Documentation

Please remember Im using I'm using Xcode 6.3.1 (6D1002)

Oscar Alencar
Oscar Alencar
2,798 Points

Hey Dominic, thanks for the help. But already have tried this and still not working. I would like to send a image for you, but this forum does not permit. My code is:

let apiKey: String = "2d4cf93e6463982e6726d2eb3785f50c" let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/") let forecastURL = NSURL(string: "-1.453550,-48.476567", relativeToURL: baseURL) let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil) println (weatherData!)

if I put a bang after baseURL, the error still persisting.. =(

I'm experiencing the same after the update I'm using Xcode 6.3.1 (6D1002). The JSON is not being parsed to human readable.

let baseURL     = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/"  )
let forecastURL = NSURL(string:"40.866863,-74.068604", relativeToURL: baseURL)
let weatherData = NSData(contentsOfURL: forecastURL!)

The preview looks something like this.

<7b226c61 74697475 6465223a 34302e38 36363836 332c226c