我正在tableView中传递json,但是当我向下滚动几次时,它显示消息“致命错误:在展开可选值时意外发现nil”

let imageCache = NSCache<AnyObject, AnyObject>()

class CustomImageView: UIImageView {

    var imageUrlString: String?

    func loadImageUsingUrlString(_ urlString: String) {

        imageUrlString = urlString

         let urlEncoded = urlString.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)

        let url = URL(string: urlEncoded!)

        image = nil

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


        // crush here down line
        URLSession.shared.dataTask(with: url!, completionHandler: {(myData, respones, error) in

            if error != nil {
                print(error ?? 0)
                return
            }

            DispatchQueue.main.async(execute: {

                let imageToCache = UIImage(data: myData!)

                if self.imageUrlString == urlString {
                    self.image = imageToCache
                }

                imageCache.setObject(imageToCache!, forKey: urlString as AnyObject)
            })

        }).resume()
    }

}

ios - 向下滚动时出现致命错误-LMLPHP

最佳答案

URLSession.shared.dataTask(with:completionHandler:)括在if-let子句中,如下所示:

if let url = url {
    URLSession.shared.dataTask(with: url, completionHandler: {(myData, respones, error) in

        if error != nil {
            print(error ?? 0)
            return
        }

        DispatchQueue.main.async(execute: {

            let imageToCache = UIImage(data: myData!)

            if self.imageUrlString == urlString {
                self.image = imageToCache
            }

            imageCache.setObject(imageToCache!, forKey: urlString as AnyObject)
        })

    }).resume()
}

注意:也尽量不要使用force-unwrapping!)。相反,在任何可能的地方使用optional bindingif-let
编辑
由于您可能是新的optionals,我发布了可能的崩溃处理代码。虽然我没有更改逻辑,但我修复了可能与崩溃相关的代码:
let imageCache = NSCache<AnyObject, AnyObject>()

class CustomImageView: UIImageView {

    var imageUrlString: String?

    func loadImageUsingUrlString(_ urlString: String) {

        imageUrlString = urlString

        guard let urlEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
            print("Encoding not done")
            return
        }

        let url = URL(string: urlEncoded)

        image = nil

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


        if let url = url {
            URLSession.shared.dataTask(with: url, completionHandler: {(myData, respones, error) in

                if error != nil {
                    print(error ?? 0)
                    return
                }

                if let myData = myData {
                    DispatchQueue.main.async(execute: {

                        let imageToCache = UIImage(data: myData)

                        if self.imageUrlString == urlString {
                            self.image = imageToCache
                        }

                        if let imageToCache = imageToCache {
                            imageCache.setObject(imageToCache, forKey: urlString as AnyObject)
                        }
                    })
                }

            }).resume()
        }
    }

}

关于ios - 向下滚动时出现致命错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45953107/

10-12 22:49