Block练习

#import 
void goToWorkDay1();
void goToWorkDay2();
typedef int (^numBlock)(int,int);
typedef void (^logBlock)();
typedef NSString * (^srtringBlock)(NSString *);
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        numBlock sum = ^(int a,int b){
            return a + b;
        };
        NSLog(@"sum = %i",sum(20,30));
        
        numBlock sub = ^(int a,int b){
            return a-b;
        };
        NSLog(@"sub = %i",sub(30,20));
        goToWorkDay1();
        goToWorkDay2();
        
        srtringBlock str = ^(NSString *name){
            name = name;
            NSLog(@"name = %@",name);
            return name;
        };
        str(@"lxc");
        
        logBlock log = ^{
            NSLog(@"log");
        };
        log();
    }
    return 0;
}
void goToWork(void (^workBlock)())
{
    NSLog(@"eat");
    workBlock();
    NSLog(@"sleep");
}
void goToWorkDay1()
{
    goToWork(^{
        NSLog(@"copy code");
    });
}
void goToWorkDay2()
{
    goToWork(^{
        NSLog(@"look code");
    });
}

你可能感兴趣的:(Block练习)