In the project development, I often check the iOS closure how to write, because its syntax is too weird, the two languages are written differently, often confusing, simply record the commonly used writing method.
A closure is a block of code that can contain free (unbound to a specific object) variable; these variables are not defined within this code block or in any global context, but are defined in the context in which the code block is defined (local variables) . The term "closure" comes from a combination of two: the block of code to execute (since free variables are included in the code block, these free variables and the objects they reference are not released) and provide bindings for free variables. Computing environment (scope).
typedef NSString *(^blockCallback)(NSString *parameter1, NSString *parameter2);
//callback closure for evaluation after success
typealias callbackFunc = (_ orderId:String ) -> Void
var evaluationBlockCallback:callbackFunc?
>First way Use statement
@property (nonatomic,strong) blockCallbackType blockCallbackName;
_blockCallbackName = ^(NSString *parameter1,NSString *parameter2){
return parameter1;
};
>The second way does not use the statement
@property (nonatomic,strong) NSString *(^blockCallbackName2)(NSString *parameter1, NSString *parameter2);
_blockCallbackName2 = ^(NSString *parameter1,NSString *parameter2){
return parameter1;
};
let blockCallbackName:String = {(parameter1:String,parameter2:String) ->String in
return parameter1
}("parameter1","parameter2")
-(void)functionUseBlock:(NSString* (^)(NSString *parameter1, NSString *parameter2))callBackOne callBackTwo:(blockCallbackType)callBackTwo{
if (callBackOne) {
callBackOne(@"parameter1",@"parameter2");
}
if (callBackTwo) {
callBackTwo(@"parameter1",@"parameter2");
}
}
func functionUseBlock(callBack:(String,String) ->String) -> Void {
print(callBack("parameter1","parameter2"))
}
[self functionUseBlock:^NSString *(NSString *parameter1, NSString *parameter2) {
return parameter1;
} callBackTwo:^NSString *(NSString *parameter1, NSString *parameter2) {
return parameter1;
}];
self.functionUseBlock = { (parameter1, parameter2) -> String in
return parameter2
}
The Swift version has a lot of abbreviations based on parameters and return values, so I won't write them here.
2019 Unicorn Enterprise Heavy Glour Recruitment Python Engineer Standard >>> This article is written in 2014.09.25 Recently IOS Development New Programming Language Swift is relatively large ...
Recently, in the development of the SWIFT project, it involves the processing of the date and time. The demand is like this. It is necessary to convert a strings of a date format type into Date type. ...
__block int val = 0; NSLog(@"val plus0 %p", &val);//0x0 void (^blk)(void) ; NSLog(@"blk plus0 %p", &blk); blk=[^{ ++val; NSLog(@"val plus1 %p", &val);//0x2 } ...
IOS Objective-C (OC) block is detailed, the content is transferred from the Booth Black Horse Course One. Introduction 1.Block is a data type. 2. Block is a data type. 3. BLOCK's statement 1). Althoug...
table of Contents version Swift calls Objective-C Objective-C call Swift Simple Analysis version Xcode 11.3.1 Swift 5.1.3 Swift calls Objective-C Create a new Swift project, and then create a new Obje...
Warehouse Address:https://github.com/tal-tech/sonar-swift Welcome to STAR. Introduction Static code scan is a way to detect the project code, and can scan the code without running the code, can scan t...
The swift notes I used to do before were all sorted on onenote. I recently thought of sorting out blogs. It is also convenient for me to find them. I can use them as a document. Closures are similar t...
Let's take a look at the comparison between Object-c's block and Swift's closure. <<<The following mainly compares the methods used by the two: Object-c declares a block that uses a block in ...
1, the closure in swift is equivalent to the block in objective-c. 2. Grammatical structure: objective-c:Return value (^ closure name) (parameter type parameter name) = ^ (parameter type parameter nam...