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

NSString.stringWithContentsOfURL doesn't exist in Swift

Hi there,

Following Pasans course and at: Using Our JSON data video he uses the mentioned statement to get the response content.

It seems that this is deprecated in the new version of Swift?

The documentation has the info on Objective-C use but nothing for Swift.

Am I missing something here?

3 Answers

Mohsen Lotfi
Mohsen Lotfi
2,694 Points

As mentioned in teachers note, the NSString initialiser has been modified and following the updated version

NSString(contentsOfURL: NSURL, encoding: UInt, error: NSErrorPointer)

Your code should look like this

var urlContents = NSString(contentsOfURL: Location, encoding: NSUTF8StringEncoding, error: nil)

Didn't see that before. Was checking under the notes but it didn't say anything.

Now I know. Thanks

Jeanne Merle
Jeanne Merle
3,390 Points

In the Documentation, you also have this note. It's a new convenience init() method who takes an URL and return a String.