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

stringWithContentsOfURL(_:encoding:error) is unavailable: use object construction 'NSString(contentsOfURL:encoding:error

I am getting "stringWithContentsOfURL(_:encoding:error) is unavailable: use object construction 'NSString(contentsOfURL:encoding:error:)'

let baseURL = "https://api.forecast.io/forecast/"
let encodedPoints = locationPoints.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

let forecastURL = NSURL(string: "\(baseURL)\(apiKey)/\(encodedPoints)")
let sharedSession = NSURLSession.sharedSession()

let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL( forecastURL!,
     completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
         var urlContents = NSString.stringWithContentsOfURL(location, encoding: NSUTF8StringEncoding, error: nil)
         println(urlContents)
     })
downloadTask.resume()

I've been having issues with those functions in that part of the course too. It seems Xcode 6.1 changed something and their code is no longer correct. Perils of learning a brand new language I suppose.

What helped me get past one of the several errors I started seeing after updating to 6.1 was to go to the Apple Swift docs, it seems Apple has changed the syntax of the function.

Thank you Andrew, Yes, I was looking at the documentation. I am used to looking at http://php.net documentation or javascript documentation. I am having a hard time following the Apple documentation. I wish they provided some examples so I can see overall syntax of the object, or method I am referencing.

2 Answers

Marko Rapaic
Marko Rapaic
13,871 Points

Hi Stephen, I believe the class method "NSData.stringWithContentsOfURL" has been deprecated. I used the following.

NSString(contentsOfURL: <#NSURL#>, encoding: <#UInt#>, error: <#NSErrorPointer#>)

Here is an example of my code:

    private let apiKey = "apikey"
    private let locationPoints = "longitude,latitude"

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let baseURL = "https://api.forecast.io/forecast/"
        let encodedPoints = locationPoints.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! // This method returns an optional, we need to force unwrap

        if let forecastURL = NSURL(string: "\(baseURL)\(apiKey)/\(encodedPoints)") { // We have to optionally bind because this method returns an optional and we need to act on the value it returns.

        let sharedSession = NSURLSession.sharedSession()

        let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in

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


        })

            downloadTask.resume()


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

        }

Thank you for the help Marko. It looks like Apple has simplified NSString so you do not have to refer to several methods. Is the above statement accurate?

Marko Rapaic
Marko Rapaic
13,871 Points

Hi Stephen,

No problem - It works for me so far, at least enough to get me past that part of the challenge - funnily enough the next step does away with the NSData class method in favour of the one I posted.

Sam Matthews
Sam Matthews
8,276 Points

Thanks Marko,

Your change for urlContents works a treat. As you say, always challenging when working with a new language.