简化通知中心的使用
说明
1. 简化通知中心,笔者曾经实现过不需要移除的通知中心,基于弱引用集合(相对于强引用集合如NSArray,NSDictionary等)编写,本例子并无太多新意
2. 简化的通知中心由一个对象组织控制,不需要你手动移除被监听的对象,这是唯一的一点点有新意的地方
3. 在处理接收通知对象的地方,笔者提供了一个方法来简化取值操作,相对应的,发送通知消息的时候,被发送的对象也请使用指定的格式(都是用weak修饰,无需担心被持有)
源码
https://github.com/YouXianMing/NotificationCenter
//
// NotificationCenterObject.h
// NotificationCenter
//
// Created by YouXianMing on 15/7/15.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface PostObject : NSObject
@property (nonatomic, strong) NSString *flag;
@property (nonatomic, weak) id data;
/**
* 便利构造器初始化对象
*
* @param flag 识别标签
* @param data 数据
*
* @return 实例对象
*/
+ (instancetype)postObjectWithFlag:(NSString *)flag data:(id)data;
/**
* 处理通知中心事件
*
* @param sender 通知中心的通知对象
*
* @return 实例对象
*/
+ (instancetype)accessNotificationCenterObject:(id)sender;
@end
///////////////////////////////////////////////////////////////////////////////////////////
@interface NotificationCenterObject : NSObject
/**
* === 必须先使用此方法进行初始化 ===
*
* 初始化通知中心
*
* @param observer 观察对象
* @param name 注册标签
* @param selector 执行用selector
*
* @return 实例对象
*/
+ (instancetype)withObserver:(id)observer
registerName:(NSString *)name
selector:(SEL)selector;
/**
* 给指定注册标签发信息
*
* @param name 注册标签
* @param postData 发送的对象
*/
+ (void)postToRegisterName:(NSString *)name data:(PostObject *)postData;
@property (nonatomic, readonly, strong) NSString *name;
@property (nonatomic, readonly, weak) id observer;
/**
* 移除
*/
- (void)remove;
/**
* 重新添加上
*/
- (void)reAdd;
@end
///////////////////////////////////////////////////////////////////////////////////////////
/**
* 创建通知对象
*
* @param flag 标签
* @param data 传递的数据
*
* @return 通知对象
*/
static inline PostObject * POST_OBJECT(NSString *flag, id data) {
return [PostObject postObjectWithFlag:flag data:data];
}
/**
* 处理通知的对象
*
* @param sender 接收的通知对象
*
* @return 通知对象
*/
static inline PostObject * ACCESS_POST_OBJECT(id sender) {
return [PostObject accessNotificationCenterObject:sender];
}
/**
* 发送通知消息
*
* @param name 指定的标签
* @param postObject 通知的对象
*
*/
static inline void POST_MSG_TO(NSString *name, PostObject *postObject) {
[NotificationCenterObject postToRegisterName:name data:postObject];
}
/**
* 注册通知中心
*
* @param name 指定的标签
* @param observer 被注册的对象
* @param selector 执行的句柄
*
* @return 实例对象
*/
static inline NotificationCenterObject * REGISTER_MSG_CENTER_WITH(NSString *name, id observer, SEL selector) {
return [NotificationCenterObject withObserver:observer registerName:name selector:selector];
}
//
// NotificationCenterObject.m
// NotificationCenter
//
// Created by YouXianMing on 15/7/15.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import "NotificationCenterObject.h"
@implementation PostObject
+ (instancetype)postObjectWithFlag:(NSString *)flag data:(id)data {
PostObject *postObject = [PostObject new];
postObject.flag = flag;
postObject.data = data;
return postObject;
}
+ (instancetype)accessNotificationCenterObject:(id)sender {
PostObject *postData = nil;
if ([sender isKindOfClass:[NSNotification class]]) {
NSNotification *notiData = sender;
if ([notiData.object isKindOfClass:[PostObject class]]) {
postData = notiData.object;
}
}
return postData;
}
@end
@interface NotificationCenterObject ()
@property (nonatomic, strong) NSString *name;
@property (nonatomic, weak) id observer;
@property (nonatomic) SEL observerSelector;
@property (nonatomic) BOOL haveAddObserver;
@end
@implementation NotificationCenterObject
+ (void)postToRegisterName:(NSString *)name data:(PostObject *)postData {
[[NSNotificationCenter defaultCenter] postNotificationName:name object:postData];
}
+ (instancetype)withObserver:(id)observer registerName:(NSString *)name selector:(SEL)selector {
NotificationCenterObject *notificationCenterObject = [NotificationCenterObject new];
notificationCenterObject.name = name;
notificationCenterObject.observer = observer;
notificationCenterObject.observerSelector = selector;
notificationCenterObject.haveAddObserver = YES;
[[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:name object:nil];
return notificationCenterObject;
}
- (void)remove {
if (self.haveAddObserver == YES && _name && _observer) {
self.haveAddObserver = NO;
[[NSNotificationCenter defaultCenter] removeObserver:_observer name:_name object:nil];
}
}
- (void)reAdd {
if (self.haveAddObserver == NO && _name && _observer) {
self.haveAddObserver = YES;
[[NSNotificationCenter defaultCenter] addObserver:_observer selector:_observerSelector name:_name object:nil];
}
}
- (void)dealloc {
if (self.haveAddObserver == YES) {
NSLog(@"已经释放");
[[NSNotificationCenter defaultCenter] removeObserver:_observer name:_name object:nil];
}
}
@end
细节
用内联函数将OC风格函数简化为C风格函数,能够稍微缩短代码编写长度,其实并无实用价值,装的成分居多。