Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 like sophisticated marco or code-generators. Is it possible to create custom directives for code-generation purposes? One possible use is generating methods for CoreData.

I'm thinking not, because I've never seen anything about it, but my world isn't the world.


Followup Question:

Jonathan mentioned below that it is possible to write your own preprocessor and this begs the question of how. Currently, #define SYMBOLIC_CONSTANT 102 will replace all instances of the characters SYMBOLIC_CONSTANT with the characters 102 in the file before the files moves on to the compiler.

I know it XCode you can add a "Run Script Phase" to a Targets build process. So I could write a script to find my custom preprocess directives like '$coredata' and then have the script generate a new file that with the characters $coredata replaced with some characters of code. But from what I understand of XCode's build process you can't feed altered files into the Compiler Sources phase. The files are specified and locked by the IDE.

Has anyone done something similar? I know it's possible with 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'll start reading Apple's XCode Documentation...

Thanks for the responses!

like image 237
Tobias Avatar asked Feb 12 '11 00:02

Tobias


2 Answers

While accepted answer is right, there is a partial hacky solution to this kind of a problem, which libextobjc library adopts. Consider this code, you will find the definitions like the following there:

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

Such definition allows using weakify keyword in the following form:

id foo = [[NSObject alloc] init];
id bar = [[NSObject alloc] init];

@weakify(foo, bar);

The author of library explains it here:

Since the macros are intended to be used with an @ preceding them (like @strongify(self);), the try {} soaks up the symbol so it doesn't cause syntax errors.


Updated later

From now on libextobjc uses @autoreleasepool to "soak up the symbol".

like image 58
Stanislav Pankevich Avatar answered Nov 13 '22 00:11

Stanislav Pankevich


Your thinking is correct: it is impossible to do this in your code. The only way to add more @-directives is via the compiler itself. Even if you went to all that trouble, I can almost guarantee that the syntax highlighting support for them is hard-coded into an Xcode configuration file somewhere.

Oh, and if you were considering the use a pre-processor macro, it is my understanding that the @ character is illegal in pre-processor macros.

Edit: I ran a test, and I am correct. Using the @ character in a C preprocessor macro is illegal. They follow the same rule as variable names.

like image 29
Carter Allen Avatar answered Nov 13 '22 00:11

Carter Allen