Is it possible to create custom directives in Objective-C?

Objective-C has directives like:

  • @interface
  • @implementation
  • @end
  • @protocol
  • @property
  • @synthesize

I think of these things, such as complex marco or code generators. Is it possible to create custom directives for code generation? One possible use case is to create CoreData methods.

I do not think because I have never seen anything about it, but my world is not the world.


Follow up question:

Jonathan mentioned below that you can write your own preprocessor, and this asks the question of how to do this. Currently, #define SYMBOLIC_CONSTANT 102 will replace all instances of the SYMBOLIC_CONSTANT characters with the characters 102 in the file before the files go to the compiler.

I know that Xcode you can add โ€œRun Script Phaseโ€ to the Targets build process. Therefore, I could write Script to find my custom preprocess directives, such as "$ coredata", and then create a new Script file, which with $ coredata characters is replaced by some code characters. But from what I understand during the Xcode build process, you cannot transfer modified files to the compiler source phase. Files are listed and locked using the IDE.

Has anyone done something like this? I know that this is possible with an external build system, but to be honest, I'm not at that level of understanding. I donโ€™t know the technical details of what the Build and Run button does.

In the meantime, I will start reading the Apple Xcode documentation ...

Thanks for answers!

+7
source share
4 answers

Your thinking is correct: this cannot be done in code. The only way to add more @ -directives is through the compiler itself. Even if you run into this whole problem, I can almost guarantee that the syntax highlighting support for them is hardcoded in the Xcode configuration file.

Oh, and if you were considering using a macroprocessor before a processor, I understand that the @ symbol is illegal in preprocessor macros.

Edit: I checked the test, and I'm right. Using the @ symbol in a C preprocessor macro is illegal. They follow the same rule as variable names.

+3
source

While the accepted answer is right, there is a partial hacker solution to this problem that the libextobjc library accepts. Consider this code , you will find the definitions as shown below:

 #define weakify(...) \ try {} @finally {} \ metamacro_foreach_cxt(ext_weakify_,, __weak, __VA_ARGS__) 

This definition allows you to use the weakify keyword in the following form:

 id foo = [[NSObject alloc] init]; id bar = [[NSObject alloc] init]; @weakify(foo, bar); 

The author of the library explains this here :

Because macros are intended to be used with the preceding @ symbol (for example, @strongify (self);), try {} absorbs the symbol so that it does not cause syntax errors.


Updated later

Now libextobjc uses @autoreleasepool to "absorb character".

+5
source

Do you mean within Objective-C? No, because he canโ€™t recognize your new keywords. You can write a preprocessor to detect @whatever and convert it to code, but if you tell us exactly what you would like to do, we can offer a more efficient or optimal approach.

+3
source

It's impossible. These are keywords built into Objective-C. Just because before it @ does not make them different from other keywords.

0
source

All Articles