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 trialDeepa Laxmi
3,035 PointsError : 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.
}
}
5 Answers
Deepa Laxmi
3,035 PointsI 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
6,354 PointsOh good catch. I did the same thing on accident because it autocompletes. To be clear for others, you should be using contentsOfURL, not contentsOfFile.
Ramesh VJ
2,201 PointsThe Error got fixed when I used
let weatherData = NSData(contentsOfURL: forecastURL!)
Ryan Jin
15,337 PointsYou 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
Deepa Laxmi
3,035 PointsThank you for your help :) but it is still showing the same error even after I imported Foundation .
Ryan Jin
15,337 PointsAre you working on the Treehouse challenge or your own program?
Deepa Laxmi
3,035 PointsI am working on video tutorial code of treehouse.
Ramesh VJ
2,201 PointsI'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
Ramesh VJ
2,201 PointsI'm using Xcode 7
Deepa Laxmi
3,035 PointsDeepa Laxmi
3,035 PointsPasan Premaratne please help me out. Thanks in advance