Difference between completion handler and blocks: [iOS]

I am confused with both the completion handler and the blocks while I use them in swift and objective-c. And when I look for blocks in swift on google, it shows the result for the completion handler! Can someone tell me what is the difference between a completion handler and blocks regarding swift and objective-c?

+7
ios objective-c block swift completionhandler
source share
3 answers

Here you can easily distinguish between blocks and completion handlers. In fact, both blocks see below.

Blocks:

Blocks are a language-level function added to C, Objective-C, and C ++ that allow you to create separate code segments that can be passed to methods or functions as if they were values. Blocks are Objective-C objects, which means that they can be added to collections, such as NSArray or NSDictionary.

  • They can be executed later, and not when the code that was implemented is executed.
  • Their use ultimately leads to much cleaner and more accurate codes, since they can be used instead of delegation methods written simply in one place and do not apply to many files.

Syntax: ReturnType (^ blockName) (parameters) see example:

int anInteger = 42; void (^testBlock)(void) = ^{ NSLog(@"Integer is: %i", anInteger); // anInteger outside variables }; // calling blocks like testBlock(); 

Block with argument:

 double (^multiplyTwoValues)(double, double) = ^(double firstValue, double secondValue) { return firstValue * secondValue; }; // calling with parameter double result = multiplyTwoValues(2,4); NSLog(@"The result is %f", result); 

Completion handler:

While a completion handler is a way (method) to implement a callback function using blocks.

A completion handler is nothing more than a simple block declaration passed as a parameter to a method that should call back later.

Note. The completion handler should always be the last parameter in the method. A method can have as many arguments as you want, but it should always have a completion handler as the last argument in the parameter list.

Example:

 - (void)beginTaskWithName:(NSString *)name completion:(void(^)(void))callback; // calling [self beginTaskWithName:@"MyTask" completion:^{ NSLog(@"Task completed .."); }]; 

An example using UIKit class UIKit .

 [self presentViewController:viewController animated:YES completion:^{ NSLog(@"xyz View Controller presented .."); // Other code related to view controller presentation... }]; 

 [UIView animateWithDuration:0.5 animations:^{ // Animation-related code here... [self.view setAlpha:0.5]; } completion:^(BOOL finished) { // Any completion handler related code here... NSLog(@"Animation over.."); }]; 
+16
source share

Blocks : Obj-c

 - (void)hardProcessingWithString:(NSString *)input withCompletion:(void (^)(NSString *result))block; [object hardProcessingWithString:@"commands" withCompletion:^(NSString *result){ NSLog(result); }]; 

Closure : Swift

 func hardProcessingWithString(input: String, completion: (result: String) -> Void) { ... completion("we finished!") } 

closing completion here , for example, is just a function that takes an argument string and returns void.

Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and in lambdas in other programming languages.

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

Shutters are first-class objects, so they can be nested and passed (like blocks in Objective-C). In Swift, functions are just special case closures.

+4
source share

In short: completion handlers are a way to implement callback functions using blocks or closures. Blocks and closures are pieces of code that can be passed to methods or functions as if they were values ​​(in other words, “anonymous functions” that we can give names and pass them).

+3
source share

All Articles