When I declare a property, I can put various attributes in the declaration, which will have special meaning for the compiler:
@property (nonatomic, retain) NSNumber *consumption;
Here, nonatomic
and retain
are attributes. Is it possible to add a custom attribute, and be able to check for the existance of this attribute at runtime ? For instance:
@property (nonatomic, retain, test) NSNumber* consumption;
I am basically using for a construct that can replace the use of attributes as I know them from C#/.NET - so alternative suggestions are also welcomed.
A custom attribute is an additional property that you can define to help describe business glossary assets. Labels are keywords that you can define to help describe any type of asset in the metadata repository. Users can use both custom attributes and labels to refine a search in the business glossary.
AttributeUsage has a named parameter, AllowMultiple , with which you can make a custom attribute single-use or multiuse. In the following code example, a multiuse attribute is created. In the following code example, multiple attributes of the same type are applied to a class. [Author("P.
The data type of the value of the custom attribute can be text, predefined values, date, number, or relationship.
If you want all the methods and properties to be available for Objective-C, you can use the @objcMembers attribute. 1 2 3 4 5 6. @objcMembers class SomeClass { func doSomething() { print("hello!") } } In that way, it will be available to the objective-c code without adding @objc specifically to the methods.
You can't add attributes to @property()
without modifying the compiler.
Note that, in general, grubbing even the existing attributes of @property
declarations at runtime is quite thoroughly discouraged. The runtime does offer an API via which you can do so, but it is not intended for general purpose use and will likely change in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With