setter and getter as objc_setAssociatedObjec [duplicate]
-
22-12-2019 - |
Question
I do not understand what mean this style and are there any benefits for standard setter/getter?
- (UIViewController*)myVC {
return objc_getAssociatedObject(self, kMJPopupViewController);
}
- (void)setMyVC:(UIViewController *) myVC {
objc_setAssociatedObject(self, kMyVC, myVC, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
Solution
You can only have member variables -- which back "standard" properties' getters and setters -- in the main @interface
and in the class extension (the category with no name). Using associated objects is especially useful in a class's category because it lets you pretend the class has a backing variable anyway.
See also libextobjc, which has a synthesizeAssociation() macro, which makes it near trivial to add @properties inside categories.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow