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 trialDana Griffin
5,870 PointsNSData dataWithContentsOfURL - Swift
I believe that this method was changed since the making of the video. If anyone is having trouble try this:
<code> class func dataWithContentsOfURL(_ aURL: NSURL, options mask: NSDataReadingOptions, error errorPtr: NSErrorPointer) -> Self! <code>
14 Answers
Cody McClintock
9,391 PointsPasan Premaratne , What is the correct way of using the NSData.dataWithContentsOfURL in the new Xcode version using Swift?
I currently am using Version 6.1 Beta 2 (6A1030) and tried everything without any success.
Did anyone else figure this out?
Cavin Graves
Courses Plus Student 1,542 Pointslet weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)
Jeroen de Vrind
29,772 PointsIf you're willing to update... for Xcode 7 and Swift 2.0 I have the following code:
// data object to fetch weather data do { let weatherData = try NSData(contentsOfURL: forecastURL!, options:NSDataReadingOptions()) print(weatherData) } catch { print (error) }
Askin Akhan
196 PointsHi guys,
I've used Dana's code suggestion and the original suggested in the video but neither of them are working for me. With Dana's code suggestion it is asking for separators to which there is no resolution.
Any suggestions?
Thanks
Luis Matute
1,490 Pointslet weatherData = NSData(contentsOfURL: forecastURL!)
This works since the return value of forecastURL
is an optional, we need to implicitly unwrapped it
Levi Lais
5,975 PointsThis is my code and it's returning nil:
let baseURL = NSURL(string: "https:/api.forecast.io/forecast/\(apiKey)/")
let forecastURL: NSURL? = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL)
let weatherData = NSData(contentsOfURL: forecastURL!)
println(weatherData)
The app crashes when it tries to execute println(weatherData)
Sebastian Nitu
8,616 PointsJeroen de Vrind could you please help me with the Build a Simple Weather App project in Swift 2 and Xcode 7? Do you have the source somewhere or could you help me with the Xcode project. I am completely stuck and after I use your do-try syntax from your example above my console says "URLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme Error Domain=NSCocoaErrorDomain Code=256 "The file “37.8267,-122.423” couldn’t be opened." UserInfo={NSURL=37.8267,-122.423}"
I have no idea what I am doing wrong
Erik Holmbom
2,356 PointsUse let weatherData = NSData(contentsOfURL: forecastURL!) With the ! after forecastURL
thomas bethell
7,772 PointsThis is what I Have...
import UIKit
class ViewController: UIViewController {
private let apiKey = "67eb5befcd83c08736eabef9d2adb076"
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/\(apiKey)/")
let forecastURL = NSURL(string: "40.692837, -73.449052", relativeToURL: baseURL)
let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)
println(weatherData)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
(Sorry, trying to use the markdown cheatsheet to get the formatting right).
Stone Preston
42,016 Pointsto format code using markdown you need to use the backtick character which is underneath esc and above the left tab, not a single quote. I fixed it for you. ill take a look at your code and see if I notice anything
Stone Preston
42,016 Pointsfound your issue:
you cannot have spaces in URLs, you have a space between your two coordinates here:
let forecastURL = NSURL(string: "40.692837, -73.449052", relativeToURL: baseURL)
remove the space:
let forecastURL = NSURL(string: "40.692837,-73.449052", relativeToURL: baseURL)
Stone Preston
42,016 PointsEDIT: this answer was for Xcode 6.0, not 6.1. the code below is correct in 6.0, however not in 6.1
what error did you get? I was able to call the method just fine using:
let weatherData = NSData.dataWithContentsOfURL(forecastURL, options:nil, error: nil)
which is what he used in the video.
you could also use
let weatherData = NSData(contentsOfURL: forecastURL)
which saves you from having to pass in the nil options and error
Dana Griffin
5,870 PointsFirstly, it wants me to unwrap forecastURL with a bang.
Then it throws an error : 'dataWithContentsOfURL(_:options:error:)' is unavailable: use object construction 'NSData(contentsOfURL:options:error:)' <--this works for me
The problem my arise because I am using beta 2, and Apple is still tweaking the Swift language.
Stone Preston
42,016 Pointsyeah swift has been updated a lot since Xcode 6 Beta 2. I would download the newest release (6.0.1). you can view it on the app store here
Dana Griffin
5,870 PointsIm using Xcode 6.1 beta 2
Stone Preston
42,016 Pointsah, ok. you didnt declare forecastURL as an optional right? it should just be a regular NSURL.
Dana Griffin
5,870 PointsI kinda new to reading documentation, but do you think this has something to do with it?:
New in Xcode 6.1 Beta! Swift Language!
• A large number of Foundation, UIKit, CoreData, SceneKit, SpriteKit, Metal APIs have been audited for optional conformance, removing a significant number of implicitly unwrapped optionals from their interfaces. This clarifies the nullability of their properties and arguments / return values of their methods. This is an ongoing effort.! !These changes replace T! with either T? or T depending on whether the value can be null or not respectively. If you find a case that was changed incorrectly, please file a radar and include the tag “#IUO” in the subject line.!
If you encounter a method for which the return value is incorrectly considered non-nullable, or a property that is incorrectly considered non-nullable, you can work around the problem by immediately wrapping the result in an optional:
var fooOpt: NSFoo? = object.reallyMightReturnNil() if let foo = fooOpt { ... }
Please be sure to file a bug about these cases. Please do not file feature requests about APIs that are still marked as T!. We know about them.
Regardless, I am currently downloading the GM so I can conform to the videos.
Stone Preston
42,016 Pointsyes that probably has something to do with it.
Nick Barnes
7,818 PointsHi all, I too am using Xcode 6.1 Beta 2 and the following NSData functions are broken. The give the same errors mentioned by Dana above:
NSData
.dataWithContentsOfFile
.dataWithContentsOfURL
.dataWithData
The only working function is NSData.dataWithContentsOfMappedFile
I still have Xcode v6.0 installed on my mac and the above functions work as expected so it must be a result of those changes.
Nick
Tom Elders
1,231 PointsSame issue here. Xcode 6.1 Beta 2. Dana's comment above illustrates the fix
let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)
I'm assuming Apple has changed the way Swift accesses class methods for NSData, but if anyone has a canonical explanation of what's happening here, I'd love to hear it.
Jake Johnson
6,839 PointsI just posted some code that compiles correctly under the Questions section of this video. Given that Swift has changed quite a bit, the code in the video will not work until it gets updated by the Treehouse team. My code compiles and at least will let you move forward with the course until they get a chance to rework the material. Hope it helps!
thomas bethell
7,772 PointsDid anyone find the answer to this issue? I am having the same problem THis is what I am entering...
'''swift let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil) ''' I get: "fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) "
As a response...I have tried most of what you have mentioned.
Stone Preston
42,016 Pointspost your full code please. there could be an issue with your URL
thomas bethell
7,772 PointsYour good!!!!
That was it, it is working now.
Thanks Stone! (I appreciate your responsiveness as well!!!)
Stone Preston
42,016 Pointsno problem, glad I could help!
Jason Jones
5,917 PointsHad the same issue when using the syntax in the video. I am running XCode 6.1.1. I found the following code takes care of all the errors.
let weatherData = NSData(contentsOfURL: forecastURL!)
That said, I'm still confused on the optional. I have no explicit optional declaration anywhere in my code. In playing with NSURL's various methods, I ran into the need to unwrap its absoluteString method. Has something about NSURL changed that it is now returning an optional?
Michael Reining
10,101 PointsThe following syntax is working for me:
let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)
println(weatherData)
Roberta Voulon
5,792 PointsRoberta Voulon
5,792 PointsBedankt Jeroen, your code worked for me! I was trying to make it work in Swift 2.0... hope they'll update the course soon... at least at Udacity they are on the ball and have updated all the course notes while they're updating the videos...