forked from overtake/telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTGObserverProxy.m
51 lines (41 loc) · 1.35 KB
/
TGObserverProxy.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#import "TGObserverProxy.h"
@interface TGObserverProxy ()
@property (nonatomic, weak) id target;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) id object;
@property (nonatomic) SEL targetSelector;
@end
@implementation TGObserverProxy
- (instancetype)initWithTarget:(id)target targetSelector:(SEL)targetSelector name:(NSString *)name
{
return [self initWithTarget:target targetSelector:targetSelector name:name object:nil];
}
- (instancetype)initWithTarget:(id)target targetSelector:(SEL)targetSelector name:(NSString *)name object:(id)object
{
self = [super init];
if (self != nil)
{
self.target = target;
self.name = name;
self.object = object;
self.targetSelector = targetSelector;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationReceived:) name:name object:object];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:_name object:_object];
}
- (void)notificationReceived:(NSNotification *)notification
{
__strong id target = self.target;
if (target != nil)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[target performSelector:_targetSelector withObject:notification];
#pragma clang diagnostic pop
}
}
@end