Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_BAD_ACCESS in swift while calling function

I know there are many answers about this error but I cannot seem to figure it out. I am receiving the error Thread 1: EXC_BAD_ACCESS (code=257, address=0x200000003) on the line where the function is called. My table cell view controller is as follows.

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")
}

This code calls the function loadImageUsingUrlString, which is located in my extensions.swift file. It is called for each table view cell that is loaded in order to load its image. The code for extensions.swift is as follows.

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()
}

}

Thank you in advance for any help.

like image 597
Neil Johnson Avatar asked Dec 05 '22 16:12

Neil Johnson


1 Answers

You did not set the class CustomImageView to the postImageView in the IBInspector:

description

like image 73
Bista Avatar answered Dec 24 '22 17:12

Bista