Jump to content

Add a variable to a class instance at runtime: Difference between revisions

(jq)
Line 659:
#import <objc/runtime.h>
 
static void *fooKey = &fooKey; // one way to define a unique key is a pointer variable that points to itself
static char fooKey;
 
int main (int argc, const char *argv[]) {
Line 667:
 
// set
objc_setAssociatedObject(e, &fooKey, @1, OBJC_ASSOCIATION_RETAIN);
 
// get
NSNumber *associatedObject = objc_getAssociatedObject(e, &fooKey);
NSLog(@"associatedObject: %@", associatedObject);
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.