自定义通知中心

#import <Foundation/Foundation.h>

@interface RCNotificationCenter : NSObject
+ (void)postNotificationName:(NSString *)name objectInfo:(NSDictionary *)objectInfo complement:(void (^)(id value))complement;
+ (void)addObserver:(id )observer name:(NSString *)name complement:(id (^)(id observer , NSDictionary *objectInfo))complement;
@end

#import "RCNotificationCenter.h"
#import <objc/runtime.h>
#define WeakSelf(type) __weak typeof(type) weak##type = type;
#define StrongSelf(type) __strong typeof(weak##type) strong##type = weak##type
#define LOCK() dispatch_semaphore_wait([RCNotificationCenter defaultCenter].semLock, DISPATCH_TIME_FOREVER);
#define UNLOCK() dispatch_semaphore_signal([RCNotificationCenter defaultCenter].semLock);
static void *BlockKey = "BlockKey";
static void *NameKey = "NameKey";
@interface RCNotificationCenter()

/**
 弱引用字典(已经注册过通知的)
 */
@property(nonatomic , strong)NSMapTable *mapTable;
/**
 锁
 */
@property(nonatomic , strong)dispatch_semaphore_t semLock;

@end
static RCNotificationCenter *center = nil;
@implementation RCNotificationCenter
#pragma mark - 单例
+ (instancetype)defaultCenter{
    return [[self alloc] init];
}
-(instancetype)init{
    static dispatch_once_t onceToken;
    WeakSelf(self);
    dispatch_once(&onceToken, ^{
        StrongSelf(self);
        center = [super init];
        strongself.mapTable = [NSMapTable mapTableWithKeyOptions:(NSPointerFunctionsStrongMemory) valueOptions:NSPointerFunctionsWeakMemory];
        strongself.semLock = dispatch_semaphore_create(1);
    });
    return center;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        center = [super allocWithZone:zone];
    });
    return center;
}

-(id)copyWithZone:(NSZone __unused*)zone{
    return center;
}
- (id)mutableCopyWithZone:(NSZone __unused*)zone{
    return center;
}
+ (void)postNotificationName:(NSString *)name objectInfo:(NSDictionary *)objectInfo complement:(void (^)(id value))complement{
    __block id value = nil;
    LOCK();
    NSArray<NSString *> *keys = [[RCNotificationCenter defaultCenter].mapTable keyEnumerator].allObjects;
    [keys enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj containsString:name]) {
            id observer = [[RCNotificationCenter defaultCenter].mapTable objectForKey:obj];
            id (^block)(id observer , NSDictionary *objectInfo) = objc_getAssociatedObject(observer, BlockKey);
            if (block) {
                value = block(observer , objectInfo);
                complement(value);
            }
        }
    }];
    UNLOCK();
}
+ (void)addObserver:(id )observer name:(NSString *)name complement:(id (^)(id observer , NSDictionary *objectInfo))complement{
    LOCK();

    objc_setAssociatedObject(observer, BlockKey, complement, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(observer, NameKey, name, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    NSString *key = [NSString stringWithFormat:@"%@-%@",[NSString stringWithFormat:@"%p",observer],name];
    [[RCNotificationCenter defaultCenter].mapTable setObject:observer forKey:key];
    UNLOCK();
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值