• block和代理使用对比


    demo设计:做简单的抽奖,在viewcontroller中调用代理和block去获得抽奖号码,在viecontroller中打印出来。

    代理类的.h文件

    @protocol DelegateClassDelegate;
    
    @interface DelegateClass : NSObject
    
    @property(nonatomic,weak) id<DelegateClassDelegate> delegate;
    
    -(void)dosomthing;
    
    @end
    
    @protocol DelegateClassDelegate <NSObject>
    
    -(void)DelegateClass:(DelegateClass *)delegateClass passNum:(NSUInteger)num;
    
    @end

    代理类的.m 文件

    @implementation DelegateClass
    
    -(void)dosomthing{
        
        NSUInteger random = arc4random()%100;
        [self.delegate DelegateClass:self passNum:random];
        
    }
    @end

    block 类的.h文件

    @interface BlockClass : NSObject
    
    -(void)dosomething:(void (^)(NSUInteger num))passnumblock;
    
    @end

    block类的.m文件

    @implementation BlockClass
    
    -(void)dosomething:(void (^)(NSUInteger num))passnumblock{
        
         NSUInteger random = arc4random()%100;
        passnumblock(random);
    }
    
    @end

    在viewcontroller中调用

    #import "ViewController.h"
    #import "DelegateClass.h"
    #import "BlockClass.h"
    
    @interface ViewController ()<DelegateClassDelegate>
    @property(nonatomic,strong) DelegateClass *delegateClass;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        
        [super viewDidLoad];
        self.delegateClass = [[DelegateClass alloc] init];
        self.delegateClass.delegate = self;
        
        //触发方法
        [self.delegateClass dosomthing];
        BlockClass *block = [[BlockClass alloc] init];
        [block dosomething:^(NSUInteger num) {
            NSLog(@"通过block获得的抽奖号码是%ld",num);
        }];
        [block dosomething:^(NSUInteger num) {
             NSLog(@"通过block获得的抽奖号码是%ld",num);
        }];
    }
    - (void)DelegateClass:(DelegateClass *)delegateClass passNum:(NSUInteger)num{
        NSLog(@"通过代理获得的抽奖号码是%ld",num);
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end

    打印结果:

    2016-02-27 15:50:08.017 delegateAndBlock[8338:1753403] 通过代理获得的抽奖号码是63

    2016-02-27 15:50:08.017 delegateAndBlock[8338:1753403] 通过block获得的抽奖号码是85

    2016-02-27 15:50:08.017 delegateAndBlock[8338:1753403] 通过block获得的抽奖号码是46

    总结:

    代理是一对一的实现,block可以实现一对多

    代理的使用较为麻烦,block相对简单,并且代码的逻辑更连贯,可读性强

    代理在做自定义控件时很有优势,因为代理消息触发机制比较灵活,代理可以根据用户的对代理类的操作而发送消息,但是block的消息发送只能是通过调用block类的那个对象来触发,代理像一个高级的仆人,他不仅可以帮你实现你想让他干的事,他还会主动处理他收到的从用户那里接受到的消息,但是他不能同时处理多个任务。而block则像部队,部队可以有很多个兵,但是他比较笨,你让他干什么他才干什么,他可以随时处理多个任务。

  • 相关阅读:
    Dubbo与Zookeeper、SpringMVC整合和利用(负载均衡、容错)
    英语每日阅读---2、越来越多人反对人工智能参战
    新东方雅思词汇---6.3、brilli
    智课雅思词汇---二十五、形容词后缀-ate-fic-ose-ulent-olent-ous-ulous-y
    英语每日写作---1、但是,人们在吹口哨时做得更好
    英语每日阅读---1、科学美国人60秒:如果觉得唱歌很难 那就吹口哨吧
    线段覆盖长度
    智课雅思词汇---二十四、形容词后缀-al-ial-ar-ary-ic-id-ish-ile-ine-oid-ory
    智课雅思词汇---二十三、动词性后缀-ate-fy-ish-ize
    iscroll.js的简单使用方法(总结)
  • 原文地址:https://www.cnblogs.com/mengdaxia117/p/5223083.html
Copyright © 2020-2023  润新知