这取决于您的经理和您的控制器的关系。
结论:如果 A 拥有 B 并且 B 拥有 A 而没有弱引用,则会导致保留循环。
class NetManager {
func fire(completion: (_ result: Bool) -> Void) {
completion(true)
}
}
class controler: UIViewController {
override func viewDidLoad() {
let manager = NetManager()
// In this case the Manager and Controller NOT own each other,
// And the block will release after request finished, it is ok to not use weak.
manager.fire { (result) in
}
}
let manager = NetManager()
func anOtherExample() {
// In this case the controller own the manager,
// manager own the controller, but manager will relase the controller after
// request finished, this will cause a delay to controller's relase, but it is still ok.
manager.fire { (result) in
}
}
}
如果您的管理人员的行为是这样的,那么管理人员将拥有控制器,并且当控制器拥有管理人员时它将导致保留周期。
class NetManager {
var completion: ((_ result: Bool) -> Void)?
func fire(completion: @escaping (_ result: Bool) -> Void) {
self.completion = completion
}
}
更多细节:https ://krakendev.io/blog/weak-and-unowned-references-in-swift