I want to define a macro or some other way I can generate similar variable declarations in compile-time. E.g. I want to declare 50
#define VAR_GENERATOR (var_name, var_content) NSString* var_name = @"var_content"
for(NSString* string in [stringArray count])
NSString* var_name = [NSString stringWithFormat: @"string%d", iteration];
VAR_GENERATOR (var_name, string);
NSString* string1 = @"first string from array"
What you want to do can't be done using compile-time constructs, for a number of reasons;
The fundamental reason is that the content of
var_name is unknown at compile time, so it is impossible for the compiler to know what the variable should even be named. But even if that was the case, what about variable name collisions ? What would be the stack layout if the compiler allowed this, given that the number of local variables is unknown ?
And allow me to add that even if you were able to do that, then your variable would be declared in the local scope, and would not be accessible outside of your
In your place, I'd try to get back to what you are truly wanting to accomplish, and ponder if runtime constructs are necessary.
If the answer is yes, (are you trying to build an interpreter for some language ?), then we got tools to map arbitrary names to values, such as
TL;DR : NO, maybe you can expand a bit on what you are trying to accomplish.