【发布时间】:2015-03-10 07:57:13
【问题描述】:
请看我的代码
protocol CustomViewDelegate: class {
}
class CustomView: UIView {
var button: UIButton! = {
//setup
}()
weak var delegate: CustomViewDelegate?
}
class controller: UIViewController {
var customView: CustomView! = {
//setup
}()
private func setup() {
customView.button.addTarget(self, action: "actionForButton:", forControlEvents: .TouchUpInside)
}
}
起初,我认为这段代码不会造成任何问题,因为我已经为委托设置了weak。此外,我已经这样编码一年多了,它工作得很好。
但我错了,这行代码确实会导致内存增加,这让我很惊讶。
为什么这会在 swift 中发生,它在 Objective-C 中运行良好?
有没有更好的解决方案?
谢谢。
【问题讨论】:
标签: ios swift memory-management automatic-ref-counting