xcode EXC_BAD_ACCESS 快速调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39483960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EXC_BAD_ACCESS in swift while calling function
提问by Neil Johnson
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.
我知道有很多关于这个错误的答案,但我似乎无法弄清楚。我在调用函数的行上收到错误 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")
}
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.
此代码调用函数 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()
}
}
Thank you in advance for any help.
预先感谢您的任何帮助。
回答by Bista
回答by Kevin Lieser
Your extensions.swift
is in the right Target?
你extensions.swift
是在正确的目标?
In XCode select the file in Project Navigator and check in File Inspector the section Target Membership
在 XCode 中选择项目导航器中的文件并检查文件检查器部分 Target Membership