Difference Between Completion Handler and Blocks : [iOS]

前端 未结 4 1283
遇见更好的自我
遇见更好的自我 2020-12-08 14:34

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

4条回答
  •  独厮守ぢ
    2020-12-08 15:24

    I am hope this will help.

    First Step:

    #import 
    
    @interface ViewController : UIViewController
    
    
    -(void)InsertUser:(NSString*)userName InsertUserLastName:(NSString*)lastName  widthCompletion:(void(^)(NSString* result))callback;
    
    @end
    

    Second Step :

    #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
    

提交回复
热议问题