How do I use objc_setAssociatedObject/objc_getAssociatedObject inside an object?

Declare a static variable so that you can use its address as the key.
The call to objc_setAssociatedObject takes a void* and only the address of your static variable is actually used, not the contents of a NSString… that is only wasting memory.

You just need to add:

static char STRING_KEY; // global 0 initialization is fine here, no 
                        // need to change it since the value of the
                        // variable is not used, just the address

Leave a Comment