我正在尝试在加载表单时在文本框中加载字符串。不知何故什么也没出现。有人可以向我解释为什么会这样吗?
let imageCache = NSCache<NSString, UIImage>()
class CustomImageView : UIImageView {
var imageUrlString: String?
func loadImageWithData (urlString: String) {
imageUrlString = urlString
if let imageFromCache = imageCache.object(forKey: urlString as NSString){
self.image = imageFromCache
}
image = nil
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
if let err = error {
print(err.localizedDescription)
}
if let data = data {
DispatchQueue.main.async {
let imageToCache = UIImage(data: data)
if self.imageUrlString == urlString {
self.image = imageToCache
}
imageCache.setObject(imageToCache!, forKey: urlString as NSString)
}
}
}).resume()
}
}
答案 0 :(得分:0)
我按照pstrjds的提示创建了一个最小,完整和可验证的示例。原来,我在代码的另一部分出现错误,导致文本框无法加载任何文本。非常感谢。