Saturday, December 10, 2011

objc_associate magic.

So say you've got an object that doesn't have a User Info or Tag inside it, but you really really really want to attach something to it specifically without juggling another synthesized property. Easy.
Import this:


#import <objc/runtime.h>

Then use this line to attach the object:

objc_setAssociatedObject(yourObject, (const void*)0x314, objectToAttach, OBJC_ASSOCIATION_RETAIN);

Use this line later to get the object back:

id objectThatWasAttached = objc_getAssociatedObject(yourObject, (const void*)0x314);

Trust me, you'll find a use for it.

No comments:

Post a Comment