iOS Objective-C and Swift closure (block)

Foreword

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.

Closure definition

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).

Declare a closure

typedef NSString *(^blockCallback)(NSString *parameter1, NSString *parameter2);
//callback closure for evaluation after success
    typealias callbackFunc = (_ orderId:String ) -> Void
    var evaluationBlockCallback:callbackFunc?

Define a closure

>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")

Function definition closure

-(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"))
    }

Call function closure (block)

[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
        }

end

The Swift version has a lot of abbreviations based on parameters and return values, so I won't write them here.

Intelligent Recommendation

[iOS development] SWIFT calls Objective-c code

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 ...

Date -VS-Objective-C NSDATE in iOS-SWIFT

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. ...

Vicki753's iOS Basics - Objective-C -- Block

__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 Detailed

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...

Swift for iOS Development (15)-Mixed Swift and Objective-C

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...

More Recommendation

SONARQUBE IOS code scanner (Objective-C / Swift / Infer / Sonar-Swift)

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...

Swift-18 closure and block

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...

OC block and Swift closure

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 ...

Swift closure block

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top