Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use objc_setAssociatedObject/objc_getAssociatedObject inside an object?

If I use objc_setAssociatedObject/objc_getAssociatedObject inside a category implementation to store a simulated instance variable in a setter method, how would I access the key in the getter method since any variables declared in the setter method would be outside the scope of the getter method?

Edit: To clarify, if I were to use the following pattern, where should I declare STRING_KEY so that I could use it in both the setter and the getter method.

@interface NSView (simulateVar) -(void)setSimualtedString:(NSString *)myString; -(NSString *)simulatedString; @end  @implementation NSView (simulateVar)  -(void)setSimualtedString: (NSString *)myString {     objc_setAssociatedObject(self, &STRING_KEY, myString, OBJC_ASSOCIATION_RETAIN); }  -(NSString *)simulatedString {     return (NSString *)objc_getAssociatedObject(self, &STRING_KEY); }  @end 
like image 739
leo Avatar asked May 17 '10 01:05

leo


People also ask

When to use objc_ setAssociatedObject?

Sets an associated value for a given object using a given key and association policy.

What is an associated object?

Associated Objects is part of Objective-C runtime. It allow you to associate objects at runtime. Simply, you can attach any object to any other object without subclassing. Below you can find built-in Associated Object methods in Foundation SDK I'll explain each property.

What is Objc_setassociatedobject?

Returns the value associated with a given object for a given key.


2 Answers

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 
like image 115
Brent Priddy Avatar answered Sep 18 '22 00:09

Brent Priddy


I know that this question is quit old but I think for completeness there is another way how to use associated objects worth mentioning. This solution utilizes @selector and therefore there is no need for any extra variable or constant.

@interface NSObject (CategoryWithProperty)  @property (nonatomic, strong) NSObject *property;  @end  @implementation NSObject (CategoryWithProperty)  - (NSObject *)property {     return objc_getAssociatedObject(self, @selector(property)); }  - (void)setProperty:(NSObject *)value {     objc_setAssociatedObject(self, @selector(property), value, OBJC_ASSOCIATION_RETAIN_NONATOMIC); }  @end 

(inspired by http://www.tuaw.com/2013/04/10/devjuice-better-objective-c-associated-objects/)

like image 28
zlajo Avatar answered Sep 19 '22 00:09

zlajo