Often when I am debugging and trying more than one thing, instead of commenting several lines, and uncommenting others, it is easier to use this technique. There’s no need to define any variables, it’s just for testing, although you can leave it in there,
if(true) // if I change the true to false, then the “second set of instructions” will get executed
{
do this // first set of instructions
and also this
}
else
{
do the other // second set of instructions
and this other
and some more
}
for example :
// ±--------±--------±--------±--------±--------±--------±--------±--------+
// If I wanted to test the first set of instructions, I could comment the third line :
// ±--------±--------±--------±--------±--------±--------±--------±--------+
NSString *timeStr = [self bhFormatDate:today withFormat:@"hh:mm a z"];
[timeField2 setStringValue:timeStr];
// [timeField2 setStringValue:[self bhFormatDate:today withFormat:@"hh:mm:ss a z"]];
// ±--------±--------±--------±--------±--------±--------±--------±--------+
// If I wanted to test the second set of instructions, I could comment the first two lines :
// ±--------±--------±--------±--------±--------±--------±--------±--------+
// NSString *timeStr = [self bhFormatDate:today withFormat:@"hh:mm a z"];
// [timeField2 setStringValue:timeStr];
[timeField2 setStringValue:[self bhFormatDate:today withFormat:@"hh:mm:ss a z"]];
But that is all a pain, especially if each set of instructions has multiple lines, INSTEAD I can just do :
if(true)
{ // putting in true will run this set of instructions
NSString *timeStr = [self bhFormatDate:today withFormat:@“hh:mm a z”];
[timeField2 setStringValue:timeStr];
}
else
{ // putting in false will run this set of instructions
[timeField2 setStringValue:[self bhFormatDate:today withFormat:@“hh:mm:ss a z”]];
}
I’ve used this little technique in many languages for over twenty years, it’s just for debugging, and I don’t need to comment out either set of instructions.
Whatever you test for within if ( … ) has to evaluate to either true or false anyway.
.