How can I add additional, custom information to my objective-c methods and properties? I want to retrieve this metadata later on at runtime.
I know objective-c doesn’t directly support method attributes, like in C#, but this is what I’m looking for:
[MyCustomAttribute(true)]
public void MyAwesomeMethod(int arg) {
// ...
}
The closest thing I recently found was a #define in the UIKit header files called UI_APPEARANCE_SELECTOR. Apparently Apple tags certain methods with this to be read later on.
Methods and properties are then define like this:
@property(nonatomic,retain) UIColor *tintColor UI_APPEARANCE_SELECTOR;
- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics UI_APPEARANCE_SELECTOR;
Can someone explain exactly how this works?
Thank you!
There’s no inbuilt support for tagged metadata (attributes in .NET, annotations in Java, etc.) Apple’s macro here is solely for the benefit of their developer tools, and you can’t create your own that have any meaning past the preprocessor.
GCC/LLVM define some attributes that can be attached to various symbols. For instance, you can mark a method as deprecated by tagging it with
__attribute__((__deprecated__))but there’s still no way to make a meaningful custom attribute.If you tell us what you’re trying to accomplish, maybe we can suggest an alternate approach to the problem.