NSData(contentsOfURL: url) always returning nil
playgrounds are sandboxed and it seems that there isn't an easy way to reach outside their "box". XML parsing in swift the title of this question is a bit misleading, the answer on it does answer this question
SWIFT: NSData contentsOfUrl returns nil from valid URL
In my specific case, it was the actual data in the URL that was missing... So that is why it returned nil.
Thanks guys for trying to help :)
Upgraded to Xcode 6.1.1, now NSData(contentsOfURL:) always returns nil
Rebooting
the Mac
fixed it. I hadn't rebooted
since updating to Xcode 6.1.1
.
Swift - NSURL error
The NSURL constructor you're calling has got this signature:
convenience init?(string URLString: String)
? means that the constructor may not return a value, hence it is considered as an optional.
Same goes for the NSData constructor:
init?(contentsOfURL url: NSURL)
A quick fix is:
let myProfilePictureURL = NSURL(string: "http://graph.facebook.com/bobdylan/picture")
let imageData = NSData(contentsOfURL: myProfilePictureURL!)
self.myImage.image = UIImage(data: imageData!)
The best solution is to check (unwrap) those optionals, even if you're sure that they contain a value!
You can find more infos on optionals here: link to official Apple documentation.
How to connect iOS app to mySQL database using php
Your problem is that one (or more) of your optionals is nil when you try to access it. Its important to understand optionals if you are doing swift development, I'd recommend going through the documentation.
let dataURL: NSData = NSData(contentsOfURL: NSURL(string: strURL)!)!
let strResult: String = String(data: dataURL, encoding: NSUTF8StringEncoding)!
In the code above, when you use !
you are telling swift that you are 100% sure that optional contains a value. There might be an issue in either your construction of a url NSURL(strUrl)!
or when calling the NSData(...)
constructor and unwrapping its result with !
.
Try something like this:
if let url = NSURL(string: strURL) {
if let data = NSData(contentsOfURL: url) {
var strResult = String(data: data, encoding: NSUTF8StringEncoding)
print(strResult ?? "Something Went Wrong")
}
else { // get rid of this else when you find your issue
print("Could not instantiate an NSData object out of that url")
}
}
else { // get rid of this else when you find your issue
print("Your URL is nil!, check the strUrl")
}
Here, we first unwrap the NSURL as NSURL(string) constructor returns an optional (the string you pass might be an invalid URL). We then unwrap the NSData object and then coalesce the strResult when printing it (as its also an optional).
Related Topics
Wrapping a Generic Method in a Class Extension
Swift Encode Tuple Using Nscoding
How to Generate Random Numbers Without Repetition in Swift
What Makes a Property a Computed Property in Swift
Accessor Gives the Wrong Value in Swift 1.2/2.0 Release Build Only
Flip Arfaceanchor from Left-Handed to Right-Handed Coordinate System
Swiftui Out of Index When Deleting an Array Element in Foreach
Nsjsonserialization Not Working as Expected in a Playground
Extending View with Extra Function Without Using Anyview
What Is the Swift Equivalent of -[Nsobject Description]
Add Skreferencenode/Skscene to Another Skscene in Spritekit
Split Uint32 into [Uint8] in Swift
Firebase Completion Listeners in Swift
How to Call the More Specific Method of Overloading
How to Use Trailing Closure in If Condition
Realitykit - Adding Modelentity to an Argeoanchor