Swift closure loop reference and swift loop reference
When a closure is used, the class itself holds self, and then accesses the attributes of self or self in the closure, which will lead to nausea of circular reference. The solution provided by swift is to define the capture list in the closure, and the capture list is how the closure wants to reference the captured variables. For example, the following definition defines a capture list, referencing self as an unowned reference, so that using self in Bin AO will not cause circular reference.
Let name = "cyy" lazy var someClosure: (Int, String)-> String = {[unowned self] (index: Int, stringToProcess: String) -> String in print (stringToProcess + self. name) return self. name}
// The printed value is:
//Lllcyy
//Deinit >>> <kuaixiaoyigou. LoginViewController: 0x7fde8a40acb0>
Remove capture list
Let name = "cyy" lazy var someClosure: (Int, String)-> String = {(index: Int, stringToProcess: String)-> String in print (stringToProcess + self. name) return self. name} // print value: // lllcyy does not call deinit