美文网首页
iOS 由masory之Block小记

iOS 由masory之Block小记

作者: 泛坷 | 来源:发表于2018-03-05 17:11 被阅读10次
    回调block
    -(void)changeValue:(void(^)())content;
    
    返回Block
    -(void(^)())changeValuet;
    
    
    

    函数式编程

    TextBlock *test = [TextBlock new];
        
        void(^block)(NSString *str) = test.logString;
        block(@"dddd");
    //上面两行等价于下面的一行
    //    test.logString(@"66666");
        
        
        test.logVoid();
    
    //链式编程思维
        test.logObj(@"1").logObj(@"2").logObj(@"3");
        NSLog(@"======================");
    

    TextBlock.h

    -(void(^)(NSString *string))logString;
    
    -(void(^)(void))logVoid;
    
    -(TextBlock *(^)(NSString *obj))logObj;
    

    TextBlock.m

    -(void(^)(NSString *string))logString{
        return ^(NSString *string){
            NSLog(@"%@",string);
        };
    }
    
    -(void(^)(void))logVoid{
        return ^(){
            NSLog(@"........");
        };
    }
    
    -(TextBlock *(^)(NSString *obj))logObj{
        return ^(NSString *obj){
            NSLog(@"返回对象的打印 %@",obj);
            return self;
        };
    }
    

    相关文章

      网友评论

          本文标题:iOS 由masory之Block小记

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