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 Interacting with Data From the Web Fetching Data

Error : cannot find an initialiser for type 'NSData' that accepts an argument list

I am getting error on line

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

Error : cannot find an initialiser for type 'NSData' that accepts an argument list of type '(contentsOfFile: NSURL, options: nil, error: nil)'

Thanks in advance.

here is my code

 //
//  ViewController.swift
//  Stormy
//
//  Created by Kh. Deepalaxmi on 29/07/15.
//  Copyright (c) 2015 Kh. Deepalaxmi. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    private let forecastAPIKey = "d65a97d1550c7a4f2748e50a117ef32c"

    @IBOutlet weak var currentTemperatureLabel: UILabel?
    @IBOutlet weak var currentHumidityLabel: UILabel?
    @IBOutlet weak var currentPrecipitationLabel: UILabel?

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

        let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(forecastAPIKey)/")
        let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL)

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


        /*if let plistPath = NSBundle.mainBundle().pathForResource("CurrentWeather", ofType: "plist"),
        let weatherDictionary = NSDictionary( contentsOfFile: plistPath),
        let currentWeatherDictionary = weatherDictionary["currently"] as? [String: AnyObject]
        {
            let currentWeather = CurrentWeather(weatherDictionary: currentWeatherDictionary)

            currentTemperatureLabel?.text = "\(currentWeather.temperature)"
            currentHumidityLabel?.text = "\(currentWeather.humidity) %"
            currentPrecipitationLabel?.text = "\(currentWeather.precipProbability)%"
        }*/

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Pasan Premaratne please help me out. Thanks in advance

5 Answers

I found out the reason behind the error, I passed the wrong argument inside NSData.

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

James Hoffman
James Hoffman
6,354 Points

Oh good catch. I did the same thing on accident because it autocompletes. To be clear for others, you should be using contentsOfURL, not contentsOfFile.

The Error got fixed when I used

let weatherData = NSData(contentsOfURL: forecastURL!)

Ryan Jin
Ryan Jin
15,337 Points

You might have to import the Foundation framework. Like

import Foundation

However, what you are doing is synchronize programming, which is not a good practice. I recommend you to use asynchronous programming, so that your code does not block the UI

Thank you for your help :) but it is still showing the same error even after I imported Foundation .

Ryan Jin
Ryan Jin
15,337 Points

Are you working on the Treehouse challenge or your own program?

I am working on video tutorial code of treehouse.

I'm getting the error

Error : cannot find an initializer for type 'NSData' that accepts an argument list of type '(contentsOfURL: NSURL, options: nil, error: nil)'

the line of code is

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

Please help. Thanks in advance

I'm using Xcode 7