ios - EXC_BAD_ACCESS 在调用函数时 swift

标签 ios swift xcode uitableview

我知道有很多关于此错误的答案,但我似乎无法弄清楚。我在调用函数的行上收到错误 Thread 1: EXC_BAD_ACCESS (code=257, address=0x200000003)。我的表格单元格 View Controller 如下。

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

此代码调用位于我的 extensions.swift 文件中的函数 loadImageUsingUrlString。为加载的每个 TableView 单元格调用它以加载其图像。 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()
}

}

提前感谢您的帮助。

最佳答案

您没有在 IBInspector 中将类 CustomImageView 设置为 postImageView:

description

关于ios - EXC_BAD_ACCESS 在调用函数时 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39483960/

相关文章:

快速核心数据迁移以更改属性类型

ios - 如何获取包含字典的数组

ios - 如果它引用的 ViewControllers 是 NavigationControllers,如何给 TabBarController 项目命名?

ios - 当我使用 NSPredicate 为我的 UITableView 过滤我的数组时,应用程序崩溃

ios - AVplayer 和视频文件扩展 - 导出和共享管道

iOS Reachability SCNetworkReachabilitySetCallback 当从 wifi 切换到 anthor wifi 时不回调

objective-c - Xcode 6.1 找不到 'debug view hierarchy' 按钮

xcode - swift : The smallest excutable for-loop

ios - 自动布局旋转固定位置

ios - testflight 上的应用程序名称与 iTunesConnect 不同