首页 > 代码库 > 利用block完成回调,小demo一个

利用block完成回调,小demo一个

利用block完成回调,小demo一个。闲话少说,直接上代码了!O(∩_∩)O~

TestObject.h

#import <Foundation/Foundation.h>

typedef void (^FinishBlock)(NSString *backStr);

@interface TestObject : NSObject

//能进行回调的方法
- (void)playSomeTime:(FinishBlock)block;

@end

TestObject.m

#import "TestObject.h"

@implementation TestObject

- (void)playSomeTime:(FinishBlock)block{
    
    for (int i = 0; i< 5000; i++) {
        NSLog(@"i===%d",i);
    }
    block(@"被回调的字符串O(∩_∩)O~");
}

@end

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

ViewController.m

#import "ViewController.h"
#import "TestObject.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    TestObject *obj = [[TestObject alloc] init];
    [obj playSomeTime:^(NSString *backStr){
        NSLog(@"回调完成,被返回的结果===%@",backStr);
    }];
    NSLog(@"这是同步操作,需要等待回调完成");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end