Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tod

macrumors regular
Original poster
Oct 3, 2009
162
100
Ohio
This is driving me insane. Here is my code exactly:
Code:
	int x;
	x=3;
	
	if (x=517) {
	[SoundServices playSoundWithName:@"switch-17" type:@"wav"];
	}

And it plays the sound every time. How could this be?
 
You're missing the extra "=" in the conditional.

It's actually true because the assignment of 517 to x worked.

But yeah, that kind of stuff would drive me nuts too, back in the day.
 
Thank you. I thought == was only for strings. Shows how much I have to learn.
 
You're missing the extra "=" in the conditional.

It's actually true because the assignment of 517 to x worked.

But yeah, that kind of stuff would drive me nuts too, back in the day.


Me too. Someone long ago when I first started in Borland C told me something to avoid this:

if (517 = x) {

}

I got into the habit of always putting the primitive first in comparisons such as these
 
Just to emphasize something here - if you find yourself blaming the SDK (whether XCode, Eclipse, Visual C++ etc) for some code not behaving the way you expect, it's 99.99999% certain to be a bug in your code.

(x = 517) always equates to true. ulbador's habit is a good one to get into.
 
Me too. Someone long ago when I first started in Borland C told me something to avoid this:

if (517 = x) {

}

I got into the habit of always putting the primitive first in comparisons such as these

Cool trick. I might remember that one.

As for a similar, more recent occurrence of this bug, I tried to be 'compact':

Code:
if ((some_var = do_something(blah)) < 10)
 
gcc has a warning for this. It's called Missing Braces and Parentheses in the Xcode project settings.

Warn if parentheses are omitted in certain contexts, such as when there is an assignment in a context where a truth value is expected, or when operators are nested whose precedence people often get confused about.

You should probably turn on as many warnings as you can stand.
 
Just to emphasize something here - if you find yourself blaming the SDK (whether XCode, Eclipse, Visual C++ etc) for some code not behaving the way you expect, it's 99.99999% certain to be a bug in your code.

(x = 517) always equates to true. ulbador's habit is a good one to get into.

Ha, that's so funny you say that. I worked with someone who constantly blamed the compiler whenever he ran into a bug where the fix wasn't immediately obvious.

"I just found a HUGE bug in Java/PHP" was his favorite line, at least once a week. Of course it wasn't like we were using development versions of Java or PHP.

Good times and laughs
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.