zoukankan      html  css  js  c++  java
  • iOS 通知 代理 block

    通知:“一对多”,在APP中,很多控制器都需要知道一个事件,应该用通知;

    delegate:
    1,“一对一”,对同一个协议,一个对象只能设置一个代理delegate,所以单例对象就不能用代理;
    2,代理更注重过程信息的传输:比如发起一个网络请求,可能想要知道此时请求是否已经开始、是否收到了数据、数据是否已经接受完成、数据接收失败

    block:
    1:写法更简练,不需要写protocol、函数等等
    2,block注重结果的传输:比如对于一个事件,只想知道成功或者失败,并不需要知道进行了多少或者额外的一些信息
    3,block需要注意防止循环引用:

    ARC下这样防止:
    __weak typeof(self) weakSelf = self;
      [yourBlock:^(NSArray *repeatedArray, NSArray *incompleteArray) {
           [weakSelf doSomething];
        }];

    非ARC

    __block typeof(self) weakSelf = self;
      [yourBlock:^(NSArray *repeatedArray, NSArray *incompleteArray) {
           [weakSelf doSomething];
        }];

    1
  • 相关阅读:
    计算机网络常见面试题
    字节跳动2022秋招提前批来了!!!
    IBM Watson Studio
    Git提交GitHub
    python获取股票和基金等数据
    Cloud Foundry
    微软亚洲研究院的NLP一例
    Streamlit的学习小记
    在线学习云技术相关等
    IBM云部署相关
  • 原文地址:https://www.cnblogs.com/fantasy3588/p/5391484.html
Copyright © 2011-2022 走看看