【iOS】NotificationCenter And Block

最近有朋友問(wèn)通知的底層怎么實(shí)現(xiàn),然后我大致的理了下:
1.通知是單例來(lái)的
2.通知可以注冊(cè)與注銷,說(shuō)明里面有數(shù)組在控制著
3.通知是要實(shí)現(xiàn)方法的,說(shuō)明里面有協(xié)議存在
4.通知是有設(shè)置通知接收者的,說(shuō)明這是使用了代理的

綜合上述說(shuō)明,我自己基于blcok寫(xiě)了套自己的通知:
主要是使用兩個(gè)方法

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

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

先在需要接收的地方添加上block,然后在需要傳值出來(lái)的地方發(fā)送通知(信息通過(guò)message來(lái)傳遞)

使用方法
先對(duì)應(yīng)key添加blcok

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

然后再對(duì)應(yīng)key發(fā)送消息

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

其中的key就是接收和發(fā)送的識(shí)別鑰匙,也就是對(duì)應(yīng)key發(fā)送與添加block,同時(shí)這里的block是可以添加多個(gè)的。

下面是這個(gè)通知類的代碼

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

// 發(fā)送通知
+ (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

/* --- 單例創(chuàng)建 --- */
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];
}




/* --- 發(fā)送通知 --- */
+ (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

如果有什么地方有漏洞希望大家及時(shí)指出,以便及時(shí)更改!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評(píng)論 19 139
  • 點(diǎn)擊查看原文 Web SDK 開(kāi)發(fā)手冊(cè) SDK 概述 網(wǎng)易云信 SDK 為 Web 應(yīng)用提供一個(gè)完善的 IM 系統(tǒng)...
    layjoy閱讀 14,315評(píng)論 0 15
  • 國(guó)家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說(shuō)閱讀 12,427評(píng)論 6 13
  • 把網(wǎng)上的一些結(jié)合自己面試時(shí)遇到的面試題總結(jié)了一下,以后有新的還會(huì)再加進(jìn)來(lái)。 1. OC 的理解與特性 OC 作為一...
    AlaricMurray閱讀 2,667評(píng)論 0 20
  • 【點(diǎn)石成金】20170717 學(xué)習(xí)力6 Day63 寶寶這幾天格外的粘人,我放假在家就一直粘著我,什么事都...
    葉子ya豆子閱讀 99評(píng)論 0 0

友情鏈接更多精彩內(nèi)容