美文网首页iOS
两个关于block代码的片段

两个关于block代码的片段

作者: Shelin | 来源:发表于2017-03-07 14:41 被阅读227次

    片段一、二

    @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
    

    相关文章

      网友评论

        本文标题:两个关于block代码的片段

        本文链接:https://www.haomeiwen.com/subject/oyemgttx.html