I'd like to use
#define IS_APP_FULL_VERSION NO
and the code
isAppFullVersion = IS_APP_FULL_VERSION;
to set instance variable. But is there a way to also do
#if IS_APP_FULL_VERSION == "NO"
// add some methods here
#endif
but it would give a compile error, and so is #if (IS_APP_FULL_VERSION == "NO"). Is there a way to check it against YES and NO? (check against the substitution value)
Update: it seems like > and == 0 is allowed, such as
#if IS_APP_FULL_VERSION == 0
so we can actually use 0 and 1 for false and true, and use == 1 to test, but it will be better if YES and NO can be used.
Update 2:
One possible solution turns out to be:
#define IS_APP_FULL_VERSION 1
#if IS_APP_FULL_VERSION
// add some methods here
#endif
isAppFullVersion = IS_APP_FULL_VERSION;
will all work, and we can just change 1 to 0 to toggle the code.
Your basic problem is that the macro processor #if statement only has one data type -- integer. All #if expressions are evaluated in integer, with undefined symbols being replaced with zero. String expressions (including comparisons) cannot be evaluated.
Actually, normally, I would just use a flag macro rather than a value one, since there's only two cases here:
#define APP_IS_FULL_VERSION
Either define or don't define that macro. Then, if you later wanted to include/exclude code based on that, you can use:
#ifdef APP_IS_FULL_VERSION
// Do something
#endif
(or #ifndef for the opposite case, obviously).
For the code segment, it's a simple matter of using that to select the correct code:
#ifdef APP_IS_FULL_VERSION
isAppFullVersion = YES;
#else
isAppFullVersion = NO;
#endif
I think, you're some confused comparison MACRO.
#define format is following it.
#define MACRO_NAME VALUE (VALUE is blank spaces are also fine. MACRO_NAME need is.)
if you want comparison compile-time following like it. this is comparison mean only defined vs no defined. not YES vs NO
#define IS_APP_FULL_VERSION 1
#if defined IS_APP_FULL_VERSION
NSLog(#"full-verison");
#endif
#define IS_APP_FULL_VERSION blahblah
#if defined IS_APP_FULL_VERSION
NSLog(#"full-verison");
#endif
If you want to build separate versions of. refer a following like it.
#define IS_APP_FREE_VERSION
#if defined IS_APP_FREE_VERSION
NSLog(#"free-version");
#endif
#define IS_APP_FULL_VERSION
#if defined IS_APP_FULL_VERISON
NSLog(#"full-version");
#endif
if you want comparison runtime following like it. this is comparsion normally.
#define IS_APP_FULL_VERSION 1
if(IS_APP_FULL_VERSION)
{
NSLog(#"full-verison");
}
#define IS_APP_FULL_VERSION 0
if(!IS_APP_FULL_VERSION)
{
NSLog(#"no full-version");
}
Related
Is it possible to do check if I've got an empty define? IS_EMPTY_OR_UNDEFINED is a fictive macro I just came up with.
#define constantA 0
#define constantB 1
#define constantC null
#define constantF ""
#define constantH
#if IS_EMPTY_OR_UNDEFINED(constantA)
# error constantA is defined 0 and the above if should not be true - this line should not run
#endif
#if IS_EMPTY_OR_UNDEFINED(constantB)
# error constantB is defined 1 and the above if should not be true - this line should not run
#endif
#if IS_EMPTY_OR_UNDEFINED(constantC)
# error constantC is defined null and the above if should not be true - this line should not run
#endif
#if IS_EMPTY_OR_UNDEFINED(constantF)
# error constantF is defined "" and the above if should not be true - this line should not run
#endif
#if ! IS_EMPTY_OR_UNDEFINED(constantH)
# error constantH is defined empty and the above if should not be true - this line should not run
#endif
#if defined(undefinedConstant) && ! IS_EMPTY_OR_UNDEFINED(undefinedConstant)
# error undefinedConstant is not defined and the above if should not be true - this line should not run
#endif
Checking if an expression is empty can be done (modulo some really exotic border case) but the technique is somewhat complicated. P99 has a macro that does this and which you could be using as
#if !defined(constantA) || P99_IS_EMPTY(constantA)
...
#endif
Combining this in one single macro is not allowed by the C standard. AS of C11 6.10.1 p4, the token defined is not allowed inside macros or other expressions.
You can use #ifdef MACRO or #if defined(MACRO) to test if macros are defined at all. You can also compare them, although only against integers:
#if MACRO > 5
etc. Maybe this helps you?
Edit: Although I don't know how you would check if a definition evaluates to "empty", or if this is possible.
Small problem I'm having where I have an amount of objects (enemies) on the screen at one particular time and cannot redefine their value. I set my enemies to begin with 3 on the screen.
My objective is to change the amount of enemies based on the current score.
I've attached snippets of the code below.
#define kEnemies 3
- (void) EnemyIncrease
{
if (self.score>=100) {
#define kEnemies 4
}
}
// I've also tried amongst other things
#define kEnemies 3
- (void) EnemyIncrease
{
if (self.score >=100) {
#undef kEnemies
#define kEnemies 6
}
}
Would really appreciate some assistance.
I have now changed my code to the following
int numberOfEnemies;
if (self.score>=0) {
numberOfEnemies = 3
}
else if (self.score>=100) {
numberOfEnemies = 4
}
however the issue now is that the array does not update the numberOfEnemies when the score meets the new condition.
for(int i = 0; i < numberOfEnemies; i++)
Apologies I'm still new to coding and trying to modify existing code
Macros are preprocessed, that means that they are processed before the rest of your code is even compiled.
Taking out your code, that means the preprocessor sees this (using your second example). Ultimately, the value of kEnemies is 6:
#define kEnemies 3
#undef kEnemies
#define kEnemies 6
It's not really viable to use #defines for variables, I only use them for constants.
You could use a member variable:
int numberOfEnemies;
...
if (self.score >=100)
{
numberOfEnemies = 6
}
(I removed the k prefix as this style is intended for constants)
I have an iVar named,
int DATA_IN_TRANSIT;
and I have defined several macros, e.g.
#define PLACES 0;
When I do something like the following,
if(DATA_IN_TRANSIT == PLACES)
{
NSLog(#"Make LLVM Dance!");
}
I get a compiler error (expression expected) in the line if(DATA_IN_TRANSIT == PLACES)
I don't know why it's giving me an error? Am I doing something naive?
#define PLACES 0
but without ';'
otherwise you'll get
if(DATA_IN_TRANSIT == 0;)
{
NSLog(#"Make LLVM Dance!");
}
I have made the following #defines
#define GOLD 1;
#define SILVER 2;
later in my program I set up some variables
int source, target;
They are then set to the values:
source = GOLD;
target = SILVER;
I now want to compare in an if statement:
if(source == GOLD) {...}
But the compiler complains Expected ')' before ';' token. If I change the line to
if(source == 1) {...}
There is no problem with the compiler or the behavior. How do I reference the #define in the if statement to make myself clear to the compiler?
Because you have an errant ; after the #define GOLD 1 and #define SILVER 2. Remove them, your problem will go away.
I found a comment today in a source file:
// - no longer compare BOOL against YES (dangerous!)
Is comparing BOOL against YES in Objective-C really that dangerous? And why is that?
Can the value of YES change during runtime? Maybe NO is always 0 but YES can be 1, 2 or 3 - depending on runtime, compiler, your linked frameworks?
The problem is that BOOL is not a native type, but a typedef:
typedef signed char BOOL;
#define YES (BOOL)1
#define NO (BOOL)0
As a char, its values aren't constrained to TRUE and FALSE. What happens with another value?
BOOL b = 42;
if (b)
{
// true
}
if (b != YES)
{
// also true
}
You should never compare booleans against anything in any of the C based languages. The right way to do it is to use either:
if (b)
or:
if (!b)
This makes your code much more readable (especially if you're using intelligently named variables and functions like isPrime(n) or childThreadHasFinished) and safe. The reason something like:
if (b == TRUE)
is not so safe is that there are actually a large number of values of b which will evaluate to true, and TRUE is only one of them.
Consider the following:
#define FALSE 0
#define TRUE 1
int flag = 7;
if (flag) printf ("number 1\n");
if (flag == TRUE) printf ("number 2\n");
You should get both those lines printed out if it were working as expected but you only get the first. That's because 7 is actually true if treated correctly (0 is false, everything else is true) but the explicit test for equality evaluates to false.
Update:
In response to your comment that you thought there'd be more to it than coder stupidity: yes, there is (but I still wouldn't discount coder stupidity as a good enough reason - defensive programming is always a good idea).
I also mentioned readability, which is rather high on my list of desirable features in code.
A condition should either be a comparison between objects or a flag (including boolean return values):
if (a == b) ...
if (c > d) ...
if (strcmp (e, "Urk") == 0) ...
if (isFinished) ...
if (userPressedEsc (ch)) ...
If you use (what I consider) an abomination like:
if (isFinished == TRUE) ...
where do you stop:
if (isFinished == TRUE) ...
if ((isFinished == TRUE) == TRUE) ...
if (((isFinished == TRUE) == TRUE) == TRUE) ...
and so on.
The right way to do it for readability is to just use appropriately named flag variables.
All this is true, but there are valid counter arguments that might be considered:
— Maybe we want to check a BOOL is actually YES or NO. Really, storing any other value than 0 or 1 in a BOOL is pretty incorrect. If it happens, isn't it more likely because of a bug somewhere else in the codebase, and isn't not explicitly checking against YES just masking this bug? I think this is way more likely than a sloppy programmer using BOOL in a non-standard way. So, I think I'd want my tests to fail if my BOOL isn't YES when I'm looking for truth.
— I don't necessarily agree that "if (isWhatever)" is more readable especially when evaluating long, but otherwise readable, function calls,
e.g. compare
if ([myObj doThisBigThingWithName:#"Name" andDate:[NSDate now]]) {}
with:
if (![myObj doThisBigThingWithName:#"Name" andDate:[NSDate now]]) {}
The first is comparing against true, the second against false and it's hard to tell the difference when quickly reading code, right?
Compare this to:
if ([myObj doThisBigThingWithName:#"Name" andDate:[NSDate now]] == YES) {}
and
if ([myObj doThisBigThingWithName:#"Name" andDate:[NSDate now]] == NO) {}
…and isn't it much more readable?
Again, I'm not saying one way is correct and the other's wrong, but there are some counterpoints.
When the code uses a BOOL variable, it is supposed to use such variable as a boolean. The compiler doesn't check if a BOOL variable gets a different value, in the same way the compiler doesn't check if you initialize a variable passed to a method with a value taken between a set of constants.