-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNSObject+AssociatedObjects.m
More file actions
executable file
·43 lines (35 loc) · 1.07 KB
/
NSObject+AssociatedObjects.m
File metadata and controls
executable file
·43 lines (35 loc) · 1.07 KB
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
//
// NSObject+AssociatedObjects.m
//
// Created by Andy Matuschak on 8/27/09.
// Public domain because I love you.
//
#import "NSObject+AssociatedObjects.h"
#import <objc/runtime.h>
@implementation NSObject (AMAssociatedObjects)
- (void)associateValue:(id)value withKey:(void *)key
{
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_RETAIN);
}
- (void)weaklyAssociateValue:(id)value withKey:(void *)key
{
objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_ASSIGN);
}
- (id)associatedValueForKey:(void *)key
{
return objc_getAssociatedObject(self, key);
}
- (void)performSelectorOnMainThreadOnce:(SEL)selector
{
[self associateValue:[NSNumber numberWithBool: YES] withKey: (void*)selector];
dispatch_async(dispatch_get_main_queue(), ^{
if ([self associatedValueForKey: (void*)selector]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self performSelector: selector];
#pragma clang diagnostic pop
[self associateValue:nil withKey:(void*)selector];
}
});
}
@end