Tutorial for using trailing closures in ES0en ES1en and Swift

  • 2020-06-07 05:23:53
  • OfStack

preface

During project development, you will often look up how to write iOS closures, because the syntax is so weird that the two languages are often written differently

Closure definition

A closure is a block of code that can contain free (not bound to a particular object) variables; These variables are not defined within the code block or in any global context, but in the context in which the code block is defined (local variables). The term "closure" comes from a combination of the code block to be executed (because the free variables are contained in the block, these and the objects they reference are not released) and the computing environment (scope) that provides the binding for the free variables.

Both the block in OC and the trailing closure in Swift act as a return value for parameters, known as callbacks.

1. Use of block

In OC, block is mainly divided into three types, namely

(1) _NSConcreteGlobalBlock global static,

(2) _NSConcreteStackBlock is stored on the stack and destroyed when the function is out of scope.

(3) _NSConcreteMallocBlock is saved in the heap and retainCount == 0 is destroyed.

When using block, we generally divided into the following steps:


//1. use typedef define 1 a block
typedef void(^CallBack1)(); // With no arguments  
typedef void(^CallBack2)(NSString *test); // With parameters 
//2. By property declaration 
@property (nonatomic, copy) CallBack callBack;
//3. By function method declaration 
- (void)functionCallBack:(CallBack)callBack;

In fact, if you write it more skillfully, you can also connect 1 definition:


@property (nonatomic, strong) void(^ completed1)();// With no arguments 
@property (nonatomic, strong) void(^ completed2)(NSString *test);// With parameters 
- (void) functionCallBack:(void(^)())completed1;// With no arguments 
- (void) functionCallBack:(void(^)(NSString *test))completed2;// With parameters 

2. Use of trailing closures

In Swift, a closure is a self-contained block of function code that can be used and passed around in the code, equivalent to an anonymous function.

A trailing closure is a function whose last argument is a closure, so specifying that the closure can be either written inside the function's argument parentheses or placed directly at the end of the function is like redefining the function one time.

Also, it is written in a similar way to block:


//1. use typealias define 
typealias functionBlock1 = () -> ()// With no arguments 
typealias functionBlock2 = (String) -> ()// With parameters 
//2. Declared body of function 
func blockTest1(complete: (functionBlock1)) -> () {
  complete()
}
func blockTest2(complete: (functionBlock2)) -> () {
  let re: String = "Cookie"
  complete(re)
}
//3. Using the function 
blockTest1 {}
blockTest2{ (result) in
   print(result)
}

Similarly, if you are more proficient in writing, you can also connect 1 definition:


//1. Declared body of function 
func blockTest(complete: (_ result: String)->()) -> () {
  
  let re: String = "Cookie"
  complete(re)
}
//2. Using the function 
blockTest { (result) in
   
   print(result)
}

In addition, closures in swift include escape closures, which are executed after a function returns, and are often used as function callbacks, much like Block in ES60en-ES61en. An auto-closure, when passed as an argument to a function, can be defined as an auto-closure (using the keyword @autoclosure), so that when passed as an argument, you can pass a piece of code (or a variable or expression) directly, which is automatically converted into a closure.

conclusion


Related articles: