对成员数据 task(with: completionHandler:) 的引用不明确

Ambiguous reference to member data task(with: completionHandler:)

为什么我的完成处理程序出现问题,我该如何解决?

func loadImageusingCacheWithUrlString(urlString: String) {

    self.image = nil
    if let cachedImage = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
        self.image = cachedImage
        return
    }

    let url = NSURL(string : urlString)

    URLSession.shared.dataTask(with: url!,
                               completionHandler: { (data, response, error) in
        if error != nil {
            print(error!)
            return
        }
        DispatchQueue.main.async(execute: {
            if let currImage = UIImage(data: data) {
                imageCache.setObject(currImage, forKey: urlString)
                self.image = currImage
            }

            //cell.imageView?.image = UIImage(data: data)
        })

    }).resume()

}

使用这个

let url = URL(string : urlString)

另外你可能会报错"Value of optional type 'Data?' not unwrapped",所以你应该这样写:

if let currImage = UIImage(data: data!) {
     imageCache.setObject(currImage, forKey: urlString)
     self.image = currImage
}