此篇为模仿系统NSNotificationCenter的底层实现,为我自己的构想,具体底层如何实现,不得而知
首先,自定义文件名为SFTongzhiCenter,因为通知是多对多的关系,所以先定义一个可变字典保存印射关系,
@interface SFTongzhiCenter ()
@property (nonatomic, strong) NSMutableDictionary *classMap;
@end
@implementation SFTongzhiCenter
+ (instancetype)defaultCenter{
static dispatch_once_t onceToken;
static SFTongzhiCenter *tongzhi = nil;
dispatch_once(&onceToken, ^{
tongzhi = [SFTongzhiCenter new];
tongzhi.classMap = [NSMutableDictionary dictionary];
});
return tongzhi;
}
@end
因为涉及到通知传参,所以我们自定义一个类SFNotification来保存通知,里面可以涉及三个参数,一个通知名,一个参数信息,一个通知object!因为这些参数外界只能查看,不能更改,所以都定义为readonly属性,还有一个可以快速构建类的方法!
@interface SFNotification : NSObject
@property (readonly, copy) NSNotificationName name;
@property (nullable, readonly, retain) id object;
@property (nullable, readonly, copy) NSDictionary *userInfo;
- (instancetype)initWithName:(NSString *)name object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo;
@end
在.m中可以重写属性,实现可读可写
@interface SFNotification ()
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) id object;
@property (nonatomic, copy) NSDictionary *userInfo;
@end
@implementation SFNotification
- (instancetype)initWithName:(NSString *)name object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo{
SFNotification *notification = [SFNotification new];
notification.name = name;
notification.object = object;
notification.userInfo = userInfo;
return notification;
}
@end
对外开放添加通知和发送通知以及删除通知的方法,因为通知可传参,也可不传参,故暴露两个方法,一个有参,一个无参
@interface SFTongzhiCenter : NSObject
+ (instancetype)defaultCenter;
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSString *)aName object:(nullable id)anObject;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(nullable NSNotificationName)aName object:(nullable id)anObject;
@end
首先是添加通知的逻辑,因为添加通知时,会传进来三个参数,一个是对应的实例类,一个是实例方法,还有一个是通知object,所以我们要把这三个信息都保存下来,并且通知是多对多,所以我们用字典来印射他们的关系,以通知名为key,一个可变数组为value,这个value中则保存通知aName所属的所有类信息
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSString *)aName object:(nullable id)anObject{
NSMutableArray *array = self.classMap[aName];
if (!array) {
array = [NSMutableArray array];
}
[array addObject:@{@"class":observer,@"selector":NSStringFromSelector(aSelector),@"object":anObject}];
[self.classMap setObject:array forKey:aName];
}
发送通知逻辑即为找到添加了名为aName的所有通知的类信息进行遍历,依次进行方法调用,系统底层我猜测应该直接是用objc_msgSend调用的,这里我们用系统封装好的NSInvocation类进行调用
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject{
[self postNotificationName:aName object:anObject userInfo:nil];
}
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo{
NSMutableArray *array = self.classMap[aName];
for (NSDictionary *mapDic in array) {
//当mapDic中的object与anObject一致时,才调用方法
if ([mapDic[@"object"] isEqual:anObject]) {
//NSInvocation;用来包装方法和对应的对象,它可以存储方法的名称,对应的对象,对应的参数,
/*
NSMethodSignature:签名:再创建NSMethodSignature的时候,必须传递一个签名对象,签名对象的作用:用于获取参数的个数和方法的返回值
*/
//创建签名对象的时候不是使用NSMethodSignature这个类创建,而是方法属于谁就用谁来创建
NSMethodSignature *signature = [[mapDic[@"class"] class] instanceMethodSignatureForSelector:NSSelectorFromString(mapDic[@"selector"])];
//1、创建NSInvocation对象
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.target = mapDic[@"class"];
//invocation中的方法必须和签名中的方法一致。
invocation.selector = NSSelectorFromString(mapDic[@"selector"]);
/*第一个参数:需要给指定方法传递的值
第一个参数需要接收一个指针,也就是传递值的时候需要传递地址*/
//第二个参数:需要给指定方法的第几个参数传值
//注意:设置参数的索引时不能从0开始,因为0已经被self占用,1已经被_cmd占用
SFNotification *noti = [[SFNotification alloc] initWithName:aName object:anObject userInfo:aUserInfo];
[invocation setArgument:¬i atIndex:2];
//2、调用NSInvocation对象的invoke方法
//只要调用invocation的invoke方法,就代表需要执行NSInvocation对象中制定对象的指定方法,并且传递指定的参数
[invocation invoke];
}
}
}
删除通知时,找到对应的类,在classMap中删除即可
- (void)removeObserver:(id)observer{
NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary];
[self.classMap enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSMutableArray *tmpArray = [obj mutableCopy];
for (NSDictionary *mapDic in obj) {
if ([mapDic[@"class"] isKindOfClass:[observer class]]) {
[tmpArray removeObject:mapDic];
}
}
[tmpDict setObject:tmpArray forKey:key];
}];
self.classMap = tmpDict;
}
- (void)removeObserver:(id)observer name:(nullable NSNotificationName)aName object:(nullable id)anObject{
NSMutableArray *array = self.classMap[aName];
NSMutableArray *tmpArray = [array mutableCopy];
for (NSDictionary *mapDic in array) {
if ([mapDic[@"class"] isKindOfClass:[observer class]]) {
if ([mapDic[@"object"] isEqual:anObject] || !anObject) {
[tmpArray removeObject:mapDic];
}
}
}
[self.classMap setObject:tmpArray forKey:aName];
}
由此我们的自定义通知功能就完成了,使用和体验与系统原生通知毫无区别,Done
网友评论