EDITED FOR TYPO –
How can I check one thing AND THEN another, if the first is true?
For example, say I have a shopping basket object, and I only want to do something if the basket has been created AND it isn’t empty.
I’ve tried:
if ((basket) && ([basket numberOfItems] >0))...
But the second condition is evaluated even if the first fails, resulting in a crash (presumably because i’m calling numberOfItems on an object that doesn’t exist).
I can nest them, but this seems a bit ugly, and more to the point is problematic. Say I want to do one thing if the basket exists AND isn’t empty, but another if either isn’t true. That doesn’t really work well in nested if statements.
If Objective-C is a strict superset of C (which, in my understanding, it is), then boolean evaluation short circuits with the
&&and||operators. This means that since you’re using&&, as soon as a condition fails, the remaining conditions are not even evaluated.Given your code, that means that if the object is null, then the message is never dispatched.