This repository has been archived by the owner on Jun 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
NSObject+BKAssociatedObjects.h
163 lines (119 loc) · 5.21 KB
/
NSObject+BKAssociatedObjects.h
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// NSObject+BKAssociatedObjects.h
// BlocksKit
//
#import "BKDefines.h"
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
/** Objective-C wrapper for 10.6+ associated object API.
In Mac OS X Snow Leopard and iOS 3.0, Apple introduced an addition to the
Objective-C Runtime called associated objects. Associated objects allow for the
pairing of a random key and object pair to be saved on an instance.
In BlocksKit, associated objects allow us to emulate instance variables in the
ategories we use.
Class methods also exist for each association. These associations are unique to
each class, and exist for the lifetime of the application unless set to `nil`.
Each class is a unique meta-object; the ultimate singleton.
Created by [Andy Matuschak](https://github.com/andymatuschak) as
`AMAssociatedObjects`.
*/
@interface NSObject (BKAssociatedObjects)
/** Strongly associates an object with the reciever.
The associated value is retained as if it were a property
synthesized with `nonatomic` and `retain`.
Using retained association is strongly recommended for most
Objective-C object derivative of NSObject, particularly
when it is subject to being externally released or is in an
`NSAutoreleasePool`.
@param value Any object.
@param key A unique key pointer.
*/
- (void)bk_associateValue:(nullable id)value withKey:(const void *)key;
/** Strongly associates an object with the receiving class.
@see associateValue:withKey:
@param value Any object.
@param key A unique key pointer.
*/
+ (void)bk_associateValue:(nullable id)value withKey:(const void *)key;
/** Strongly, thread-safely associates an object with the reciever.
The associated value is retained as if it were a property
synthesized with `atomic` and `retain`.
Using retained association is strongly recommended for most
Objective-C object derivative of NSObject, particularly
when it is subject to being externally released or is in an
`NSAutoreleasePool`.
@see associateValue:withKey:
@param value Any object.
@param key A unique key pointer.
*/
- (void)bk_atomicallyAssociateValue:(nullable id)value withKey:(const void *)key;
/** Strongly, thread-safely associates an object with the receiving class.
@see associateValue:withKey:
@param value Any object.
@param key A unique key pointer.
*/
+ (void)bk_atomicallyAssociateValue:(nullable id)value withKey:(const void *)key;
/** Associates a copy of an object with the reciever.
The associated value is copied as if it were a property
synthesized with `nonatomic` and `copy`.
Using copied association is recommended for a block or
otherwise `NSCopying`-compliant instances like NSString.
@param value Any object, pointer, or value.
@param key A unique key pointer.
*/
- (void)bk_associateCopyOfValue:(nullable id)value withKey:(const void *)key;
/** Associates a copy of an object with the receiving class.
@see associateCopyOfValue:withKey:
@param value Any object, pointer, or value.
@param key A unique key pointer.
*/
+ (void)bk_associateCopyOfValue:(nullable id)value withKey:(const void *)key;
/** Thread-safely associates a copy of an object with the reciever.
The associated value is copied as if it were a property
synthesized with `atomic` and `copy`.
Using copied association is recommended for a block or
otherwise `NSCopying`-compliant instances like NSString.
@see associateCopyOfValue:withKey:
@param value Any object, pointer, or value.
@param key A unique key pointer.
*/
- (void)bk_atomicallyAssociateCopyOfValue:(nullable id)value withKey:(const void *)key;
/** Thread-safely associates a copy of an object with the receiving class.
@see associateCopyOfValue:withKey:
@param value Any object, pointer, or value.
@param key A unique key pointer.
*/
+ (void)bk_atomicallyAssociateCopyOfValue:(nullable id)value withKey:(const void *)key;
/** Weakly associates an object with the reciever.
A weak association will cause the pointer to be set to zero
or nil upon the disappearance of what it references;
in other words, the associated object is not kept alive.
@param value Any object.
@param key A unique key pointer.
*/
- (void)bk_weaklyAssociateValue:(nullable __autoreleasing id)value withKey:(const void *)key;
/** Weakly associates an object with the receiving class.
@see weaklyAssociateValue:withKey:
@param value Any object.
@param key A unique key pointer.
*/
+ (void)bk_weaklyAssociateValue:(nullable __autoreleasing id)value withKey:(const void *)key;
/** Returns the associated value for a key on the reciever.
@param key A unique key pointer.
@return The object associated with the key, or `nil` if not found.
*/
- (nullable id)bk_associatedValueForKey:(const void *)key;
/** Returns the associated value for a key on the receiving class.
@see associatedValueForKey:
@param key A unique key pointer.
@return The object associated with the key, or `nil` if not found.
*/
+ (nullable id)bk_associatedValueForKey:(const void *)key;
/** Returns the reciever to a clean state by removing all
associated objects, releasing them if necessary. */
- (void)bk_removeAllAssociatedObjects;
/** Returns the recieving class to a clean state by removing
all associated objects, releasing them if necessary. */
+ (void)bk_removeAllAssociatedObjects;
@end
NS_ASSUME_NONNULL_END