Unbelievable Xcode Problem -- Totally Illogical

Discussion in 'iOS Programming' started by tod, Apr 27, 2010.

  1. tod macrumors regular

    Joined:
    Oct 3, 2009
    Location:
    Ohio
    #1
    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?
     
  2. sammich macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Location:
    Sarcasmville.
    #2
    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.
     
  3. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
  4. tod thread starter macrumors regular

    Joined:
    Oct 3, 2009
    Location:
    Ohio
    #4
    Thank you. I thought == was only for strings. Shows how much I have to learn.
     
  5. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #5

    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
     
  6. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #6
    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.
     
  7. sammich macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Location:
    Sarcasmville.
    #7
    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)
     
  8. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #8
    That would be isEqualToString: :cool:
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Or better yet, isEqualToString:
     
  10. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #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.
     
  11. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #11
    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
     

Share This Page