【发布时间】:2014-12-12 10:36:45
【问题描述】:
大家好,
我有这些源代码
let downloadTask = session.downloadTaskWithURL(url,
completionHandler: {
[weak button] url, response, error in
if error == nil && url != nil {
if let data = NSData(contentsOfURL: url) {
if let image = UIImage(data: data) {
dispatch_async(dispatch_get_main_queue()) {
if let button = button {
button.setImage(image, forState: .Normal)
}
}
}
}
此代码块尝试将来自 url 的图像放入按钮元素中。 有人可以帮我理解为什么在这个源代码块中,在completionHandler 参数列表中它使用[弱按钮]? [weak ...] 是什么意思,为什么要使用它? (我认为避免保留循环。) 谢谢各位!
【问题讨论】:
-
附注:不要勾选
error == nil,如果操作成功,不保证是nilstackoverflow.com/a/1810301/2128900
标签: ios swift completionhandler