NSURLSession.sharedSession().dataTaskWithURL 的问题

Issues with NSURLSession.sharedSession().dataTaskWithURL

我正在尝试从 Web 异步加载多个图像。看起来我找到了这个论坛 post “Loading/Downloading image from URL on Swift” 但我还有一些 "issues".

我有这个代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let newCell = tableView.dequeueReusableCellWithIdentifier("userCell") as! UserCell_TableViewCell
let selectedUser = userArray[indexPath.row]

newCell.userDescription?.text = selectedUser.getFirstName() + " " + selectedUser.getLastName()

if let userImage = selectedUser.getImage()
{
    // NO CODE
}
else
{
    newCell.userImage.downloadedFrom(self.imageArray[arrayIndex])
}

return newCell

}

extension UIImageView {

    func downloadedFrom(urlLink :String)
    {
        if let urlData = NSURL(string: urlLink) {

            NSURLSession.sharedSession().dataTaskWithURL(urlData, completionHandler: { (data, response, error) in

                if error == nil
                {

                    print("Correct URL: " + "\(urlLink)")
                }
                else
                {
                    print("Incorrect URL: " + "\(urlLink)")
                }

            }).resume()
        }
    }
}

问题

1) 为了测试,我输入了正确和错误的方向,但总是告诉我是正确的。

问题

1) 图片可以return用于其他地方吗?

2) 如何检测URL无效从而输入默认图片?

编辑

可选({ URL: http://findicons.com/files/icons/1072/face_avatars/300/a03.png } { 状态码: 200, headers { "Accept-Ranges" = 字节; 连接="keep-alive"; "Content-Length" = 32759; "Content-Type" = "image/png"; 日期 = "Fri, 23 Oct 2015 14:20:20 GMT"; "Last-Modified" = "Thu, 11 Feb 2010 10:49:27 GMT"; 服务器 = "nginx/1.1.19"; } })

可选({ URL: http://findicons.com/files/icons/1072/face_avatars/300/a01_BLABLABLA.png } { 状态码: 404, headers { 连接="keep-alive"; "Content-Encoding" = gzip; "Content-Language" = zh; "Content-Type" = "text/html; charset=utf-8"; 日期 = "Fri, 23 Oct 2015 18:01:11 GMT"; 服务器 = "gunicorn/0.17.4"; "Set-Cookie" = "csrftoken=81phPal08h22q6d87lC85TqCCOniJhhJ; expires=Fri, 21-Oct-2016 18:01:11 GMT; Max-Age=31449600; Path=/"; "Transfer-Encoding" = 身份; 变化 = "Accept-Encoding, Cookie, Accept-Language"; } })

response 是一个 NSHTTPURLResponse。如果它的 statusCode404 你没有数据,不应该再进一步。

你没有收到错误的原因是没有错误;这是一个完全有效和完整的网络来回通信。