Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I specify custom attributes on members in Objective-C?

Tags:

objective-c

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.

like image 622
driis Avatar asked May 17 '12 16:05

driis


People also ask

What are custom attributes?

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.

How can you apply custom attribute?

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.

What are the five kinds of custom attributes that can be created?

The data type of the value of the custom attribute can be text, predefined values, date, number, or relationship.

What is@ objcMembers?

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.


1 Answers

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.

like image 141
bbum Avatar answered Sep 22 '22 21:09

bbum