Is it possible to define a macro such that I can also define a variable which is a parameter? I want to write a shorthand enumeration for ranges in objective-c such that I have something like the following:
#define NSRangeEnumerate(i, range) for(i = range.location; i < NSMaxRange(range); ++i)
However, when calling this macro, obviously, I get an error since i
is not defined before calling the macro.
NSRangeEnumerate(i, range) {}
Throws and error because I need to do:
NSUInteger i;
NSRangeEnumerate(i, range) {}
What I would like to be able to do, like in a typical for
statement is the following:
NSRangeEnumerate(NSUInteger i, range) {}
How might I go about this, because if I define i
inside the macro, I realize it is defined two more times because of how the macro is written. I'd like it to be initialized once and then referenced the next two times. I hope my goal makes sense... Any help would be truly appreciated.
Edit
My current solution, although not preferred is:
#define NSRangeEnumerate(type, i, range) for(type i = range.location; i < NSMaxRange(range); ++i)
#define NSRangeEnumerate(type, i, range) for(type i = range.location; i < NSMaxRange(range); ++i)
or
#define NSRangeEnumerate(i, range) for(NSUInteger i = range.location; i < NSMaxRange(range); ++i)