Is it right to use objc_setAssociatedObject for a class object?

Is it right to use objc_setAssociatedObject for a class object?

We often model class variables using such static variables: Objective level variables of a static class, but is it possible to use related objects as alternative ones?

 objc_setAssociatedObject([self class], &STRING_KEY, myString, OBJC_ASSOCIATION_RETAIN); 
+4
source share
1 answer

Yes, the class object is a fully functional object, so you can do something with it that you can do with a regular object.

However, it’s simpler and easier to use a global variable.

ps Associating it with [self class] not the same as using a global variable, because [self class] gives you the actual class of the current object, which may change because this method is inherited by subclasses. If the global variable will always be the same variable.

+5
source

All Articles