原始闭包代码:
httpTools?.loadData({ (jsonDict : String) in
self.view.backgroundColor = UIColor.red
})
方案一
### 在闭包前面添加:weak var weakSelf : ViewController? = self
weak var weakSelf : ViewController? = self
httpTools?.loadData({ (jsonDict : String) in
weakSelf?.view.backgroundColor = UIColor.red
})
方案二
### self的使用为可选项
httpTools?.loadData({[weak self] (jsonDict : String) in
self?.view.backgroundColor = UIColor.red
})
方案三
### [unowned self] ,该方法相当于OC中的(unsafe_unretained)当引用对象被释放的时候会产生野指针
httpTools?.loadData({[unowned self] (jsonDict : String) in
self.view.backgroundColor = UIColor.red
})