If Statements

Discussion in 'iOS Programming' started by eForce, Oct 21, 2012.

  1. eForce, Oct 21, 2012
    Last edited by a moderator: Oct 21, 2012

    eForce macrumors newbie

    Joined:
    Oct 21, 2012
    #1
    1st post here. Thanks in advance.

    The following code never satisfies any of the conditions.

    Code:
    Therefore, dblAnswer is ALWAYS [_lblNum1.text doubleValue];
    
    - (IBAction)cmdOK:(UIButton *)sender {
        double dblAnswer;
        double dblAnswer2;
        
        [_txtAnswer resignFirstResponder];
        
        dblAnswer = [_lblNum1.text doubleValue];
        dblAnswer2 = [_lblNum2.text doubleValue];
        
        if (_lblOperand.text == @"+") {
            dblAnswer = dblAnswer + dblAnswer2;
        }
        if (_lblOperand.text == @"-") {
            dblAnswer = dblAnswer - dblAnswer2;
        }
        if (_lblOperand.text == @"X") {
            dblAnswer = dblAnswer * dblAnswer2;
        }
        if (_lblOperand.text == @"/") {
            dblAnswer = dblAnswer / dblAnswer2;
        }
    
        _lblResult.Text = [NSString stringWithFormat:@"%f", dblAnswer];
    }
     
  2. ChristianJapan macrumors 601

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #2
    Try the compare method as defines for NSString

    http://developer.apple.com/library/...lasses/NSString_Class/Reference/NSString.html

    You are actually comparing pointer; one is dynamic from memory management; the others are constants. Will not give meaningful result in general.

    Compare is the better/right way.


    By the way: welcome on MR!
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    The usual approach is to use NSString's isEqualToString:.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    You should probably be looking at the button itself, not the text on the button. I would suggest having each button set to invoke an IBAction.

    As is, if you decide to change what's written on the button (say you decide to replace the "X" with a "*" or some other unicode character... or maybe you decide to just use a drawing of some sort rather than text) you'll have to change both your interface file (storyboard or XIB) and your code, rather than just your interface file.

    Also, you should be using "else if", not just "if" for the other statements. As it's currently written, your code will have to compare your text against "-" even if it was already determined to be "+".

    ... a switch case actually might be your best choice...
     
  5. eForce thread starter macrumors newbie

    Joined:
    Oct 21, 2012
    #5
    agreed with the switch. It's just I read everywhere that the switch doesn't work properly with strings.
     
  6. eForce thread starter macrumors newbie

    Joined:
    Oct 21, 2012
    #6

    Thank you. So will an If ElseIf == not work? Forgive me, I'm brand new to xCode but I've programmed in VB for many years.
     
  7. moral-hazard macrumors regular

    Joined:
    Jul 27, 2009
    Location:
    Palo Alto, CA
    #7
    Something like this might work for you if you want the switch functionality:

    Code:
      char* cStr;
      [_lblOperand.text getCString:cStr maxLength:1 encoding:NSASCIIStringEncoding];
    
      switch (cStr[1]) {
        case '+':
          // do something
        case '-':
          // do something
        case '*':
          // do something
        case '/':
          // do something
      }
    
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    This code is wrong. You must pass an actual buffer address to the method, not an uninitialized pointer. Refer to the NSString class reference doc.

    I suggest the characterAtIndex: method with an index of 0.

    ----------

    An if .. else if .. using == will not work. It fails for the same reason using == with NSString fails (i.e. read the earlier posts that explain it).

    An if .. else if .. using isEqualToString: will work. It works for the same reason given in the earlier posts.

    And Objective-C doesn't have an ElseIf keyword.
     
  9. moral-hazard macrumors regular

    Joined:
    Jul 27, 2009
    Location:
    Palo Alto, CA
    #9
    My bad - thanks for the correction. I should probably compile and run these things before posting :p
     

Share This Page