Neil Johnson Neil Johnson - 1 month ago 5x
Swift Question

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

func setupArticleImage() {

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

NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, respones, error) in

if error != nil {

dispatch_async(dispatch_get_main_queue(), {

let imageToCache = UIImage(data: data!)

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

imageCache.setObject(imageToCache!, forKey: urlString)



Thank you in advance for any help.


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