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) Concurrency Using Our JSON Data

I am getting an error while executing my code

I wrote this -

let dataObject = NSData(contentsOfURL: location)
let weatherDictionary: NSDictionary = NSJSONSerialization.dataWithJSONObject(dataObject, options:nil, error: nil) as NSDictionary

and getting error -

Type NSdata? does not conform to protocol 'AnyObject'

2 Answers

Blend88 Web Studios
Blend88 Web Studios
8,056 Points

I think you need to use NSJSONSerialization.JSONObjectWithData() and not NSJSONSerialization.dataWithJSONObject()

Stone Preston
Stone Preston
42,016 Points
NSData(contentsOfURL: location)

returns an NSData optional.

however, dataWithJSONObject most likely expects a regular, non optional NSData object as its first argument

try force unwrapping the NSData object by placing a ! (bang) after the dataObject argument in the call to dataWithJSONObject

let dataObject = NSData(contentsOfURL: location)
let weatherDictionary: NSDictionary = NSJSONSerialization.dataWithJSONObject(dataObject!, options:nil, error: nil) as NSDictionary