完成处理程序与块之间的区别:[iOS]

28

在使用 SwiftObjective-C 时,我同时遇到了完成处理程序和块的问题。当我在谷歌上搜索 Swift 中的块时,它显示的是完成处理程序的结果!有人能告诉我关于 SwiftObjective-C 中完成处理程序和块的区别吗?


5
所有的完成处理程序都是代码块,但并非所有的代码块都是完成处理程序。 - Avi
4个回答

36

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

块:

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

  • 它们可以在稍后的时间执行,而不是在它们被实现的作用域代码执行时。
  • 它们的使用最终会导致更干净、更整洁的代码编写,因为它们可以代替委托方法,只在一个地方编写,而不会分散到许多文件中。

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

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

完成处理程序:

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

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

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

示例:

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

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

    NSLog(@"Task completed ..");

}];

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

10

: Obj-c

- (void)hardProcessingWithString:(NSString *)input withCompletion:(void (^)(NSString *result))block;

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

闭包: Swift

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

例如,这里的“完成闭包”只是一个接受字符串参数并返回void的函数。
闭包是一种自包含的功能块,可以在代码中传递和使用。Swift中的闭包类似于C和Objective-C中的块,以及其他编程语言中的lambda。

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

闭包是一种一等对象,因此它们可以嵌套和传递(与Objective-C中的块类似)。在Swift中,函数只是闭包的一种特殊情况。

8
简而言之:完成处理程序是使用块或闭包实现回调功能的一种方法。块和闭包是可以像值一样传递到方法或函数中的代码块(换句话说,它们是“匿名函数”,我们可以为其命名并传递)。

3

I am hope this will help.

First Step:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


-(void)InsertUser:(NSString*)userName InsertUserLastName:(NSString*)lastName  widthCompletion:(void(^)(NSString* result))callback;

@end

第二步:
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(void)InsertUser:(NSString *)userName InsertUserLastName:(NSString*)lastName widthCompletion:(void (^)(NSString* result))callback{

    callback(@"User inserted successfully");

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self InsertUser:@"Ded" InsertUserLastName:@"Moroz" widthCompletion:^(NSString *result) {

        NSLog(@"Result:%@",result);

    }];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接