简体   繁体   中英

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.

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

描述

Your extensions.swift is in the right Target?

In XCode select the file in Project Navigator and check in File Inspector the section Target Membership

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM