zoukankan      html  css  js  c++  java
  • 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则像部队,部队可以有很多个兵,但是他比较笨,你让他干什么他才干什么,他可以随时处理多个任务。

  • 相关阅读:
    [ZJOI2006]物流运输
    [SCOI2009]生日快乐
    [FJOI2007]轮状病毒
    [转载]centos 7(1611)安装笔记
    发行版Linux和麒麟操作系统下netperf 网络性能测试
    ARM64平台编译stream、netperf出错解决办法 解决办法:指定编译平台为alpha [root@localhost netperf-2.6.0]# ./configure –build=alpha
    查看linux系统是多少位,使用 getconf LONG_BIT
    https://www.jqhtml.com/30047.html strace + 命令: 这条命令十分强大,可以定位你程序到底是哪个地方出了问题
    Centos7 利用crontab定时执行任务及配置方法
    清楚自己的短板是什么 搞清楚自己的职业规划是什么
  • 原文地址:https://www.cnblogs.com/mengdaxia117/p/5223083.html
Copyright © 2011-2022 走看看