【iOS】NotificationCenter And Block

最近有朋友问通知的底层怎么实现,然后我大致的理了下:
1.通知是单例来的
2.通知可以注册与注销,说明里面有数组在控制着
3.通知是要实现方法的,说明里面有协议存在
4.通知是有设置通知接收者的,说明这是使用了代理的

综合上述说明,我自己基于blcok写了套自己的通知:
主要是使用两个方法

// 接收通知
+ (void)addBlocksObject:(NotifBlock)object WithKey:(NSString *)key;

// 发送通知
+ (void)sendNotificationWithKey:(NSString *)key Info:(NSDictionary *)message;

先在需要接收的地方添加上block,然后在需要传值出来的地方发送通知(信息通过message来传递)

使用方法
先对应key添加blcok

 [NotificationCenterBlock addBlocksObject:^(NSDictionary *info) {
        NSLog(@"%s -- %@",__func__,info);
    } WithKey:@"zzz"];

然后再对应key发送消息

 [NotificationCenterBlock sendNotificationWithKey:@"zzz" Info:@{@"JAJAAA":@"SSDDDDD",@"JAJAAAss":@"SSDDDDDss"}];

其中的key就是接收和发送的识别钥匙,也就是对应key发送与添加block,同时这里的block是可以添加多个的。

下面是这个通知类的代码

NotificationCenterBlock.h

//
//  NotificationCenterBlock.h
//  ZZZ
//
//  Created by zxw on 17/5/4.
//  Copyright © 2017年 tianshikechuang. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef void(^NotifBlock)(NSDictionary * info);

@interface NotificationCenterBlock : NSObject

// 发送通知
+ (void)sendNotificationWithKey:(NSString *)key Info:(NSDictionary *)message;

// 接收通知
+ (void)addBlocksObject:(NotifBlock)object WithKey:(NSString *)key;



+ (instancetype)shareNotificationCenterBlockWithKey:(NSString *)key;
+ (instancetype)NotificationCenterBlockWithKey:(NSString *)key;
- (instancetype)initNotificationCenterBlockWithKey:(NSString *)key;

- (void)sendNotificationInfo:(NSDictionary *)message;

- (void)addBlocksObject:(NotifBlock)object;
- (void)removeAllBlocksObject;


+ (void)removeAllBlocksObjectWithKey:(NSString *)key;

@end

NotificationCenterBlock.m

//
//  NotificationCenterBlock.m
//  ZZZ
//
//  Created by zxw on 17/5/4.
//  Copyright © 2017年 tianshikechuang. All rights reserved.
//

#import "NotificationCenterBlock.h"


@interface NotificationCenterBlock ()

@property(nonatomic, strong) NSString * key;
@property(nonatomic, strong) NSMutableDictionary * keyBlockDic;

@end



@implementation NotificationCenterBlock

/* --- 单例创建 --- */
static NotificationCenterBlock *notif = nil;
- (instancetype)init{
    
    if (notif) {
        return notif;
    }
    
    if (self = [super init]) {
        self.keyBlockDic = [NSMutableDictionary dictionary];
        notif = self;
    }
    return self;
}


- (instancetype)initNotificationCenterBlockWithKey:(NSString *)key{
    self = [[NotificationCenterBlock alloc] init];
    self.key = key;
    return self;
}

+ (instancetype)shareNotificationCenterBlockWithKey:(NSString *)key{
    return [NotificationCenterBlock NotificationCenterBlockWithKey:key];
}

+ (instancetype)NotificationCenterBlockWithKey:(NSString *)key{
    return [[NotificationCenterBlock alloc] initNotificationCenterBlockWithKey:key];
}




/* --- 发送通知 --- */
+ (void)sendNotificationWithKey:(NSString *)key Info:(NSDictionary *)message{
    
    NotificationCenterBlock * notif = [NotificationCenterBlock shareNotificationCenterBlockWithKey:key];
    [notif sendNotificationInfo:message];

}

- (void)sendNotificationInfo:(NSDictionary *)message{
    NSMutableArray * arr = [self.keyBlockDic valueForKey:self.key];
    if (!arr) {
        return;
    }
    for (NotifBlock blcok in arr) {
        blcok(message);
    }
    
    self.keyBlockDic[self.key] = arr;
}





/* --- 添加和移除 --- */
+ (void)addBlocksObject:(NotifBlock)object WithKey:(NSString *)key{
     NotificationCenterBlock * notif = [NotificationCenterBlock shareNotificationCenterBlockWithKey:key];
    [notif addBlocksObject:object];
}
+ (void)removeAllBlocksObjectWithKey:(NSString *)key{
    NotificationCenterBlock * notif = [NotificationCenterBlock shareNotificationCenterBlockWithKey:key];
    [notif removeAllBlocksObject];
}


- (void)addBlocksObject:(__autoreleasing NotifBlock)object{
    
    if (!object) return;
    
    NSMutableArray * arr = [self.keyBlockDic valueForKey:self.key];
    if (!arr) {
        arr = [NSMutableArray array];
    }
    [arr addObject:object];
    
    self.keyBlockDic[self.key] = arr;
    
}

- (void)removeAllBlocksObject{
    
    NSMutableArray * arr = [self.keyBlockDic valueForKey:self.key];
    if (arr) {
        [arr removeAllObjects];
    }
    
    self.keyBlockDic[self.key] = arr;
}


@end

如果有什么地方有漏洞希望大家及时指出,以便及时更改!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 13,957评论 0 15
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,206评论 6 13
  • 把网上的一些结合自己面试时遇到的面试题总结了一下,以后有新的还会再加进来。 1. OC 的理解与特性 OC 作为一...
    AlaricMurray阅读 2,617评论 0 20
  • 【点石成金】20170717 学习力6 Day63 宝宝这几天格外的粘人,我放假在家就一直粘着我,什么事都...
    叶子ya豆子阅读 84评论 0 0