我知道这个错误有很多答案,但我似乎无法弄明白。我在调用函数的行上收到错误Thread 1:EXC_BAD_ACCESS(code = 257,address = 0x200000003)。我的表格单元视图控制器如下。
import UIKit
class NewsTableViewCell: UITableViewCell {
@IBOutlet weak var postImageView: CustomImageView!
@IBOutlet weak var postTitleLabel:UILabel!
@IBOutlet weak var authorLabel:UILabel!
@IBOutlet weak var dateLabel: UILabel!
var article: Article? {
didSet {
postTitleLabel.text = article?.title
authorLabel.text = article?.author
dateLabel.text = article?.date
setupArticleImage()
}
}
func setupArticleImage() {
postImageView.loadImageUsingUrlString("http://theblakebeat.com/uploads/873463.jpg")
}
此代码调用函数loadImageUsingUrlString,该函数位于我的extensions.swift文件中。为每个加载的表视图单元调用它以加载其图像。 extensions.swift的代码如下。
import UIKit
let imageCache = NSCache()
class CustomImageView: UIImageView {
var imageUrlString: String?
func loadImageUsingUrlString(urlString: String) {
imageUrlString = urlString
let url = NSURL(string: urlString)
image = nil
if let imageFromCache = imageCache.objectForKey(urlString) as? UIImage {
self.image = imageFromCache
return
}
NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, respones, error) in
if error != nil {
print(error)
return
}
dispatch_async(dispatch_get_main_queue(), {
let imageToCache = UIImage(data: data!)
if self.imageUrlString == urlString {
self.image = imageToCache
}
imageCache.setObject(imageToCache!, forKey: urlString)
})
}).resume()
}
}
提前感谢您的帮助。