簡體   English   中英

調用函數時swift中的EXC_BAD_ACCESS

[英]EXC_BAD_ACCESS in swift while calling function

我知道有很多關於這個錯誤的答案,但我似乎無法弄明白。 我在調用函數的行上收到錯誤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()
}

}

預先感謝您的任何幫助。

您沒有將類CustomImageView設置為postImageView中的postImageView:

描述

你的extensions.swift是在正確的目標?

在XCode中,在Project Navigator中選擇文件,並在File Inspector中選中Target Membership

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM