完成处理程序和块之间的区别:[iOS] [英] Difference Between Completion Handler and Blocks : [iOS]

查看:121
本文介绍了完成处理程序和块之间的区别:[iOS]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 Swift Objective-C 中使用它时,我遇到了完成处理程序和块。当我在谷歌搜索 Swift 中的块时,它显示完成处理程序的结果!有人可以告诉我完成处理程序和块之间的区别是什么 Swift Objective-C

I am messed with both completion handler and blocks while I am using them in Swift and Objective-C. And when I am searching blocks in Swift on google it is showing result for completion handler! Can somebody tell me what is the difference between completion handler and blocks with respect to Swift and Objective-C ?

推荐答案

在这里,您可以轻松区分块和完成处理程序,实际上两个块都是块,请参阅下面的详细信息。

Here you can easily differentiate between blocks and completion handlers in fact both are blocks see detail below.

块:

块是添加到C,Objective-C和C ++的语言级功能,允许您可以创建不同的代码段,这些代码段可以传递给方法或函数,就像它们是值一样。块是Objective-C对象,这意味着它们可以添加到NSArray或NSDictionary等集合中。

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


  • 它们可以在以后执行,而不是在执行它们的
    范围的代码时执行。

  • 他们的使用最终会导致更清洁,更整洁的代码
    写作,因为它们可以代替委托方法使用,只需在一个地方编写
    而不是传播到许多文件。

语法: ReturnType(^ blockName)(参数)参见示例

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

int anInteger = 42;

void (^testBlock)(void) = ^{

    NSLog(@"Integer is: %i", anInteger);   // anInteger outside variables

};

// calling blocks like
testBlock();

阻止参数:

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

完成处理程序:

尽管完成处理程序是使用块实现回调功能的一种方法(技术)。

Whereas completion handler is a way (technique) for implementing callback functionality using blocks.

完成处理程序只不过是作为参数传递的简单块声明一个需要在以后进行回调的方法。

A completion handler is nothing more than a simple block declaration passed as a parameter to a method that needs to make a callback at a later time.

注意:完成处理程序应该始终是方法中的最后一个参数。方法可以包含任意数量的参数,但始终将完成处理程序作为参数列表中的最后一个参数。

示例:

- (void)beginTaskWithName:(NSString *)name completion:(void(^)(void))callback;

// calling
[self beginTaskWithName:@"MyTask" completion:^{

    NSLog(@"Task completed ..");

}];

使用 UIKit 类方法的更多示例。

More example with UIKit classes methods.

[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..");
                     }];

这篇关于完成处理程序和块之间的区别:[iOS]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆