1, closure

Swift The closure is simplified :

* Inferring parameter and return value types by context
* Implicitly returns the closure of a single expression , That is, the closure of single expression can be omitted return keyword
* Parameter name abbreviation
* Follow (Trailing) Closure grammar
Let's take a look at an example of sorting , Descending order of arrays
let usernames = ["Wangwu", "Lisi", "Xiaoming", "Zhangsan"] func backWards(s1:
String, s2: String) -> Bool { return s1 > s2 } let resultName1 =
usernames.sorted(by: backWards) //resultName1: ["Zhangsan", "Xiaoming",
"Wangwu", "Lisi"]
 1.1 Closure expression syntax
{ (parameters) -> returnType in statements }
1.2  Implicit return of single expression closure

Single line expression closures can be omitted return Keyword to implicitly return the result of a single line expression
let resultName2 = usernames.sorted { s1, s2 in s1 > s2 }
1.3 Parameter name abbreviation
let resultName3 = usernames.sorted { $0 > $1 }
1.4  Functional closure  
let resultName4 = usernames.sorted(by: >)
2. Capture value (Capturing Values)

Closures can capture constants or variables in the context in which they are defined . Even if the original scope that defines these constants and variables no longer exists , Closures can still refer to and modify these values within the body of a closure function .

3. Closures are reference types (Closures Are Reference Types)

Just like a class , It is also a reference type

4. Trailing closure (Trailing Closures))

A trailing closure is a closure expression written after a function bracket , The function supports calling it as the last parameter :
let numReult2 = caculateTwoNumbers(num1: 3, num2: 4) { $0 * $1 }
print(numReult2) func caculateTwoNumbers(num1: Int, num2: Int, CaluFunction:
(Int, Int) -> Int) -> Int{ return CaluFunction(num1, num2) }
5. Escape closure (@escaping)
func mainFunc(){ // Call function doSomething(paramClosure: {print("hello")})
doSomething(paramClosure:{print("word!")}) // Escape call closure for closurePrama in
functionArray { closurePrama() } // Non escaping closure someFunctionWithNonescapingClosure {
(a) in print(a) } } // Declare an array of functions var functionArray: [() -> Void] = []
// Defines a function that receives closure parameters , If we define a non escape function func doSomething(@noescape paramClosure:() -> Void)
There will be compilation errors func doSomething(paramClosure:@escaping () -> Void){ // Put the parameters in the array , For escape calls
functionArray.append(paramClosure) } // Non escaping closure default @noescape It can be omitted func
someFunctionWithNonescapingClosure(closure: (_ num:Int) -> Void) { let a = 1
closure(a) }
6,noescape Right and wrong escape .

@noescape Keyword Code plays a role of annotation : To illustrate a closure parameter , The closure parameter is similar to this API It's synchronous , It's only here API Called in . As long as it's time API End of operation , The closure's life cycle ends . in other words , The closure can't escape from the closure API The palm of my hand . Ha ha ha ha ! It's important for compilers and API For the caller : The compiler optimizes the code , and API Callers can be assured of bold use of the code API, You don't have to worry about creating a reference loop to use the capture list . At the same time, when you call instance variables or instance methods, you can not use them. "self."

      however ! How to use this @noescape tagging , It needs the right posture !

       The above discussion , Only when the closure is temporary , That is, it has not been used API Any other external attribute or global variable is held !!
func withLock(@noescape perform closure: () -> Void) { myLock.lock() closure()
myLock.unlock() }
In  Objective-C
- (void)performWithLock:(__attribute__((noescape)) void (^)())block { //
exposed as @noescape to Swift [myLock lock]; block(); [myLock unlock]; }
Interview questions : call Masonry Of block Why not weak?

The reason is to use the stack block, It's all for use NS_NOESCAPE modification block. The compiler does some optimizations accordingly , For example, remove some redundant pairs self
Capture of ,retain,release operation .
- (NSArray *)mas_makeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker
*make))block; - (NSArray *)mas_updateConstraints:(void(NS_NOESCAPE
^)(MASConstraintMaker *make))block; - (NSArray
*)mas_remakeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;
 

Technology