joffd joffd - 5 months ago 22
Objective-C Question

If && not working

I have looked at every similar question i can find but i am scratching to work out why my if && stament isn't working.

if ((cEventStatus) && (ddayDif = 0)) {
NSLog(@"upDateCountersWorking");
[self upDateCountersWorking];

}else if ((cEventStatus) && (ddayDif > 0 )){
NSLog(@"currentlyWorkingAddDays");
[self currentlyWorkingAddDays:&ddayDif];

}else if ((!cEventStatus) && (ddayDif = 0)){
NSLog(@"upDateCountersResting");
[self upDateCountersResting];

}else if ((!cEventStatus) && (ddayDif > 0)){
NSLog(@"currentlyRestingAddDays");
[self currentlyRestingAddDays:&ddayDif];
}else{
NSLog(@"final else");
}


I originally didn't have enough () around each statement and only & instead of &&, from the other questions i found i made the changes to come to the above code. But still no success.

In desperation i have added the following code to confirm the variables and am getting the expected results.

if (cEventStatus) {
NSLog(@"YES status");
}else{
NSLog(@"NO status");
}

if (ddayDif > 0) {
NSLog(@"greater than 0");
}else{
NSLog(@"less than 0");
}


But the if else never gives the expected result, always falls to the final else regardless of variable values.

I am sure this will be a simple issue but any help is appreciated.

Answer

This line:

if ((cEventStatus) && (ddayDif = 0)) {

isn't checking the value of ddayDif, it's setting it (to zero) - which isn't what you want.

In each of your checks you should be using == to test the value (instead of = to set it).