NSData(contentsOfURL) is always nil in PlayGround

I've searched on the forums here and on the internet and have found quite a few posts regarding this behavior, but have yet to find a solid solution. I'm utilizing the following code in a Playground.


import UIKit

var strUrl = "https://www.apple.com/ipad/home/images/social/og.jpg?201505061144"
var url = NSURL(string: strUrl)

if let data = NSData(contentsOfURL: url!)
{
    var img = UIImage(data: data)
}
else
{
    print("No data")
}
if let data = NSData(contentsOfURL: url!, options: nil, error: nil)
{
    var img = UIImage(data: data)
}
else
{
    print("No data")
}


I get both "no data" statements in the results pane. I'm ruling out the security transport issues because it's a call to an Apple https location. The direct URL works just fine.


I'm using xCode 6.4. Any suggestions?

Clearly, your first step is to let the second NSData call return the error it's encountering. (Don't use the first form at all. The version without an error parameter is obsolete.)


The equivalent code in Swift 2 works in an Xcode 7 playground, though it does produce a sandboxing error message that may or may not be related.


The other thing to do here is try it in a OS X playground, so that you don't have to go through the simulator.

Exactly the same code copied from the original post worked and returned an image of size 1200x1200 for each UIImage, tested in Xcode 6.4 on OS X 10.10.3 .

NSData(contentsOfURL) is always nil in PlayGround
 
 
Q