蓝鸥iOS从零基础到精通就业-OC语言入门 Block

  • iOS从零基础到精通就业 Objective-C
  • CSDN观看地址:http://edu.csdn.net/course/detail/3347
  • Block
  • #import 
    
    int number1 = 10000;
    
    int sum(int a,int b);
    int sum(int a,int b)
    {
         number1 = 100;
        return a+b;
     
    }
    
    int main(int argc, const char * argv[]) {
        
        number1 = 100;
        //block语法
        //^block的标示符
        /*
          返回值 (^名字)(参数)= ^(参数){
    
         }
         */
        
        
       __block int number = 10;
        void(^myBlock)() = ^(){
            NSLog(@"这是一个最简单的block");
            //block内部不能直接使用局部变量的 需要加__block 可以直接使用全局变量
            number = 100;
            number1 = 1000000;
        };
        myBlock();
        
        
        int(^sumBlock)(int,int) = ^(int a,int b){
            return a+b;
        };
        NSLog(@"%d",sumBlock(10,20));
        
        
        int(^maxBlock)(int,int) = ^(int a,int b){
            return a>b?a:b;
        };
        NSLog(@"%d",maxBlock(30,50));
        
        
        typedef int(^thisBlock)(int,int);
        
        thisBlock block1 = ^(int a,int b){
            return a-b;
        };
        
        thisBlock block2 = ^(int a,int b)
        {
            return a*b;
        };
    
        
        
        
            return 0;
    }
    
    


你可能感兴趣的:(iOS学习笔记)