As we know, we can add a variable in Objective-C using a category and runtime methods like
objc_setAssociatedObject
and objc_getAssociatedObject
. For example:
#import <objc/runtime.h>
@interface Person (EmailAddress)
@property (nonatomic, readwrite, copy) NSString *emailAddress;
@end
@implementation Person (EmailAddress)
static char emailAddressKey;
- (NSString *)emailAddress {
return objc_getAssociatedObject(self,
&emailAddressKey);
}
- (void)setEmailAddress:(NSString *)emailAddress {
objc_setAssociatedObject(self,
&emailAddressKey,
emailAddress,
OBJC_ASSOCIATION_COPY);
}
@end
But does anybody know what does objc_getAssociatedObject
or objc_setAssociatedObject
do?
I mean, where are the variable we add to the object(here is self
) stored? And the relationship between variable and self
?