• 初学Objective


    一、block声明

          1、无参数,无返回值: void (^sayHi)();

          2、有参数,有返回值: NSInteger (^operateOfValue)(NSInteger num);

               block的声明:返回值类型(^block变量名)(参数列表)

           脱字符(^)是块的语法标记

    二、block实现

          block变量名 = ^返回值类型(参数列表)

          1、  sayHi = ^{

                    NSLog(@"你好");

                 };

           2、 operateOfValue = ^ NSInteger (NSInteger num) {

                    return - num;

                 };

           注意:block的声明和实现可以写在一起,例如:

           NSInteger (^maxValue)(NSInteger num1, NSInteger num2) = ^ NSInteger (NSInteger num1, NSInteger num2){

                return  num1 > num2 ? num1 : num2;

            };

    三、block调用

                                               block变量名(参数)    

          NSLog(@"相反数 = %ld", operateOfValue(- 10));

    四、block作为方法的参数

         1、创建一个Person类

         2、Person.h  声明               block作为参数    返回值     参数 

                              + (void) personWithExecute:(void (^)(void)) block;

         3、Person.m实现

              + (void)personWithExecute:(void (^)(void))block {

                       if (block) {      // 如果block存在

                            block();

             }

        }

         4、main.m中调用

         [Person personWithExecute:^{ 

                NSLog(@"打招呼");

            }];

          注意: 程序运行过程 ---》 先走类方法,在实现文件中走block(),回调 -- NSLog

    五、block对参数的访问

           1、block对其外面的局部变量,只能读,如果非要对其做修改,需要在定义外部参数是在其前面加上__block(两个下划线),例如:

             __block int  a = 10;

                 void (^block)(void) = ^{

                      a = 30;

                      NSLog(@"block -  %d", a);

                 };

                 block ();

            2、block对于对象类型,可读可写,可以直接进行修改,例如:

                NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1", @"2", nil];

                void (^arrayBlock)(void) = ^{

                       [array addObject:@"3"];

                       NSLog(@"arrayBlock -- %@", array);

                 };

                 arrayBlock();

    六、typedef  block

         block代码块定义和实现看起来都有很大一块,可以用typedef重命名:

         示例1:

         1、Person.h:

         typedef  block返回类型(^)重命名名字(参数列表)

         typedef NSInteger (^SUMBLOCK)(NSInteger a, NSInteger b);

         + (NSInteger)sumOfNum1:(NSInteger)num1 andNum2:(NSInteger)num2 sumBlock:(SUMBLOCK)block;

         2、Person.m :

         + (NSInteger)sumOfNum1:(NSInteger)num1 andNum2:(NSInteger)num2 sumBlock:(SUMBLOCK)block {

                return block(num1, num2);

          }

          示例2:

          typedef void(^SAYHI)(void);    // 相当于定义一个类型

          SAYHI sayHi = ^{

                NSLog(@"你好");

            };

            sayHi();

  • 相关阅读:
    spring开发_Spring+Hibernate_HibernateDaoSupport
    java开发_STMP邮箱客户端_发送邮件
    struts2开发_userlogin_模拟用户登录
    spring开发_邮箱注册_激活_获取验证码
    MFC笔记(1)
    MFC笔记(2)菜单
    wpf控件开发基础(5) 依赖属性实践
    wpf控件开发基础(3) 属性系统(2)
    wpf控件开发基础(2) 属性系统(1)
    Caliburn笔记依赖注入容器(wpf框架)
  • 原文地址:https://www.cnblogs.com/ciciCassie/p/5796260.html
Copyright © 2020-2023  润新知