• 第一篇献给你:Block的回调


    回调:在一处声明和定义,有调用权限。在另一处实现,无调用权限。

    关于将block做为方法的参数进行传递:

    给出了block的实现内容

    1.发送消息queryContactOlineStatusResult

    2.此时指针进入方法queryContactOlineStatusResult:内部,此时不需理会做为参数出现的block的实现部分,完全无视它就好,可以将他们看做参数A  ,仅此而已。(只是恰好在此处实现而已,与此时的上下文语境没有半点关系)然后执行方法queryContactOlineStatusResult(A)的方法体。

       
        [[ContactManager defaultManager] queryContactOlineStatusResult:^(NSMutableArray *resultArray, NSMutableDictionary *resultDictionary) { //实现
            _onlineStatusDic.dictionary = [NSDictionary dictionaryWithDictionary:resultDictionary];
            if (weakSelf.dataArray.count) {
                [weakSelf reloadTable];
            } else {
                [weakSelf autoTriggerHeaderRefresh];
            }

        }// 在此处的block仅为实现部分,整体的实现作为一个参数,并不会在此处执行block。

    ];

    好了,回调出现于此,方法体中终于出现了你:

    “result(_onlineStatusArray, _onlineStatusDictionary);”需要执行的block!!

    此时回调(可认为是系统实现的回调动作)之前的block实现!进行运算

     //此时给出了参数A的实际名称为result。

    - (void)queryContactOlineStatusResult:(void (^)(NSMutableArray *, NSMutableDictionary *))result  //定义
    {
        if (_onlineStatusArray && _onlineStatusDictionary) {
            if (result) {
                result(_onlineStatusArray, _onlineStatusDictionary);                 //回调上述block的实现部分,在此处执行,对_onlineStatusDic.dictionary进行赋值。然后[weakSelf reloadTable]加载table,,或重新请求[weakSelf autoTriggerHeaderRefresh]。然后回调完成。
            }
        } else {
            NSMutableArray *resultArray = [NSMutableArray array];
            NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
            NSString *originFilePath = [[NSBundle mainBundle] pathForResource:@"OnlineStatus" ofType:@"plist"];
            NSArray *originArray = [[NSArray alloc] initWithContentsOfFile:originFilePath];
            for (NSDictionary *itemDic in originArray) {
                LXOnlineState *tempModel = [[LXOnlineState alloc] init];
                tempModel.name    = [itemDic valueForKey:@"name"];
                tempModel.icon    = [itemDic valueForKey:@"icon"];
                tempModel.stateID = [itemDic valueForKey:@"state"];
                [resultArray addObject:tempModel];
                [resultDictionary setValue:tempModel forKey:tempModel.stateID];
            }
            _onlineStatusArray = [NSMutableArray arrayWithArray:resultArray];
            _onlineStatusDictionary = [NSMutableDictionary dictionaryWithDictionary:resultDictionary];
            if (result) {
                result(_onlineStatusArray, _onlineStatusDictionary);
            }
        }
    }

    转一篇:http://www.cnblogs.com/ygm900/p/3633131.html (简单、粗暴、易懂)

                   对Block回调机制进行改写,让她还原成函数调用最本质的样子

    1.声明并创建block

      void(^callBack)(NSMutableArray *, NSMutableDictionary *);

      callBack = ^(NSMutableArray * resultArray, NSMutableDictionary * resultDictionary)

            {

              _onlineStatusDic.dictionary = [NSDictionary dictionaryWithDictionary:resultDictionary];
                    if (weakSelf.dataArray.count) {
                             [weakSelf reloadTable];
                    } else {
                             [weakSelf autoTriggerHeaderRefresh];
                        }

                  

           };

    2.发送消息

          [ [ContactManager defaultManager] queryContactOlineStatusResult:callBack];

    3.方法实现:

    - (void)queryContactOlineStatusResult:callBack //形参

    {
        if (_onlineStatusArray && _onlineStatusDictionary) {
            if (callBack) {
                callBack(_onlineStatusArray, _onlineStatusDictionary);
            }
        } else {
            NSMutableArray *resultArray = [NSMutableArray array];
            NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
            NSString *originFilePath = [[NSBundle mainBundle] pathForResource:@"OnlineStatus" ofType:@"plist"];
            NSArray *originArray = [[NSArray alloc] initWithContentsOfFile:originFilePath];
            for (NSDictionary *itemDic in originArray) {
                LXOnlineState *tempModel = [[LXOnlineState alloc] init];
                tempModel.name    = [itemDic valueForKey:@"name"];
                tempModel.icon    = [itemDic valueForKey:@"icon"];
                tempModel.stateID = [itemDic valueForKey:@"state"];
                [resultArray addObject:tempModel];
                [resultDictionary setValue:tempModel forKey:tempModel.stateID];
            }
            _onlineStatusArray = [NSMutableArray arrayWithArray:resultArray];
            _onlineStatusDictionary = [NSMutableDictionary dictionaryWithDictionary:resultDictionary];
            if (callBack) {
                callBack(_onlineStatusArray, _onlineStatusDictionary);
            }
        }
    }

     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    Django
    python django框架学习
    Http Header里的Content-Type
    Python 头部 #!/usr/bin/python 和 #!/usr/bin/env python的区别
    关于“编译型语言”和“解释性语言”的区别
    Axure XMind整理交互思路
    异常处理
    re模块
    模块
    正则表达式**************************
  • 原文地址:https://www.cnblogs.com/liuziyu/p/4169835.html
Copyright © 2020-2023  润新知