Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa touch URL的内容返回为零_Cocoa Touch_Swift_Facebook Graph Api - Fatal编程技术网

Cocoa touch URL的内容返回为零

Cocoa touch URL的内容返回为零,cocoa-touch,swift,facebook-graph-api,Cocoa Touch,Swift,Facebook Graph Api,我正在做一个fb请求,当我试图将URL的内容转换为NSData时,它返回为nil?我不知道哪里出了问题,我的代码是: func fbRequestCompletionHandler(connection:FBRequestConnection!, result:AnyObject!, error:NSError!){ if let gotError = error{ //got error } else{

我正在做一个fb请求,当我试图将URL的内容转换为NSData时,它返回为nil?我不知道哪里出了问题,我的代码是:

 func fbRequestCompletionHandler(connection:FBRequestConnection!, result:AnyObject!, error:NSError!){
        if let gotError = error{
            //got error
        }
        else{

            let email = result["email"]
            let firstName = result["first_name"]
            let userFBID = result["id"]
            println(userFBID) //PRINTS THE CORRECT ID
            let userImageURL = "https://graph.facebook.com/\(userFBID)/picture?type=small"

            let url = NSURL(fileURLWithPath: userImageURL)

            let imageData = NSData(contentsOfURL: url!)

            println(imageData) //PRINTS AS NIL
}
}

采用URL的
NSData
init
方法的定义如下:

init?(contentsOfURL url: NSURL)
表示它返回一个可选类型,因此返回值可以是
nil
。您可以通过以下方式确定问题所在:

var error: NSError?
if let imageData = NSData(contentsOfURL: url!, options: NSDataReadingOptions(0), error: &error) {
    // Success
}
else {
    println("An error occurred: \(error.localizedDescription)")
}