iOS

两个关于block代码的片段

2017-03-07  本文已影响227人  Shelin

片段一、二

@interface XLViewController ()

@property (nonatomic, copy) void (^testBlock)();
@property (nonatomic, copy) void (^anotherTestBlock)(XLViewController *controller);

@end
@implementation XLViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // 片段一
    WeakObj(self);
    
    self.testBlock = ^ {
        StrongObj(self);
        [strongself doSomething];
       
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
            [strongself doAnotherThing];
        });
    };

    self.testBlock();

    // 片段二
    self.anotherTestBlock = ^(XLViewController *controller){
        
        [controller doSomething];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
            [controller doAnotherThing];
        });
    };
    
    self.anotherTestBlock(self);

}

- (void)doSomething {
    NSLog(@"doSomething");
}

- (void)doAnotherThing {
    NSLog(@"doAnotherThing");
}

- (void)dealloc {
    NSLog(@"-- XLViewController -- dealloc --");
}

@end
上一篇下一篇

猜你喜欢

热点阅读