What is a closure?
Closures are blocks of code that can contain free (unbound to specific) variables that are not defined within the code block or in any global context, but are defined in the environment in which the code block is defined (local variables).
The term "closure" comes from the combination of the code blocks to be executed (because the free variables are contained in the code block, these free variables and the objects they refer to are not freed), and the Computing environment (scope) that provides the binding for the free variable.
In swift, Swift's closure resembles the block in OC, and the block in OC is similar to an anonymous function, which is used to define functions.
Either the block in OC or the closure in Swift is actually used to save a piece of code that executes when needed
How to define a closure:
Swift
| The code is as follows |
Copy Code |
Creates a closure that passes 2 parameters, and the return value is of type int var add = {(A:int,b:int)-> Int in Return a + b } You can also write this var add = ({(a:int,b:int)-> Int in Return a + b }) You can also write this var add = () {(a:int,b:int)-> Int in Return a + b } |
You cannot write self without self in swift, but in a closure, you must write self
Shorthand for closures: if the closures have no parameters and return values, you can delete the things in the previous (including in)
| The code is as follows |
Copy Code |
var closure = {()-> Void in Print ("This is a closed package") } can be abbreviated to var closure = { Print ("This is a closed package") } Yes, you can. The closure is passed to the function as an argument Swift Func-Second: (A:int,b:int)-> (Int)) { Print ("This is the first method") Let num = second (a:10,b:5) Print ("\ (num)") } (A, B) in Print ("second method") Return a + b } /* Output results: This is the first method. A second method 15 */ |
If the function receives only one parameter, the closure can be written directly behind the ()
| The code is as follows |
Copy Code |
Func Add (Num:int, sub: (a:int,b:int)-> Int) { Print ("Execute add func") Let num2 = Sub (a:10,b:5) let result = num + num2 Print ("num + num2 = \ (Result)") } Add {(A, B)-> Int in Return A-b } /* Output results: Executed the Add Func num + num2 = 15 */ |
Problems with closures circular references
As long as the circular reference is involved, there is the issue of resource release.
There is no dealloc function in swift, but there is deinit this destructor.
| The code is as follows |
Copy Code |
| Import Uikit Use attributes to save the closure, self.finished to save the closure, and then use self in the closure, so a circular reference appears var finished: (()-> ())? Class Viewcontroller:uiviewcontroller { Override Func Viewdidload () { Super.viewdidload () Add { Print ("This is in the main process") Self.view.backgroundColor = Uicolor.greencolor () } } Func Add (sub: ()-> ()) { Print ("Execute add func") } Deinit { Print ("Destroy Here") } } |
How to solve it?
Turn self into a weak reference.
| The code is as follows |
Copy Code |
| Import Uikit Use attributes to save the closure, self.finished to save the closure, and then use self in the closure, so a circular reference appears var finished: (()-> ())? Class Viewcontroller:uiviewcontroller { Override Func Viewdidload () { Turn self into a weak reference weak var weakself = self Super.viewdidload () Add { Print ("This is in the main process") weakself!. View.backgroundcolor = Uicolor.greencolor () } } Func Add (sub: ()-> ()) { Print ("Execute add func") } } |