Conditional Statement

Discussion in 'Mac Programming' started by hurleyint1386, Dec 30, 2008.

  1. hurleyint1386 macrumors member

    Joined:
    Sep 4, 2006
    #1
    I'm having some issues with a simple conditional statement.

    Code:
    int x;
    x = (int)txtX.text;
    if (x > 4)
    {
        x = 4;
    }
    
    I'm getting the value of txtX.text from a text field, converting it to an integer, then checking to see if it's greater than 4. When I run this, the application build successfully, but when ever it hits this statement, it freezes. Any ideas?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    This isn't enough information to help you. How do you know the program is frozen there? Have you put a print statement after this that is never run? Have you attached with a debugger? What sort of thing is txtX? What sort of thing is txtX.text? Do you understand that (int) is a typecast and not converting from text to an int like atoi would, or the message intValue would, depending on what txtX.text is, and will instead cast the address of a char * or NSString * to an int? What language are you working in?

    Give us some extra info and we will be glad to help.

    -Lee
     
  3. hurleyint1386 thread starter macrumors member

    Joined:
    Sep 4, 2006
    #3
    Ok, this is an iPhone App, but is still written in Objective-C. txtX.text is a textField in the interface. I'm trying to get the value of that field, convert it to an integer value, then print it to a label. When I hit my button, it pauses, then doesn't change. If I comment out the conditional, it prints the proper number
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    NSTextField is an NSControl, which responds to intValue with the current value of the field interpreted as an int. You could then compare the result to 4 or assign it to an int variable, etc. I don't know if this will fix the freezing, as I am not clear why or if it is freezing, but it should be a little closer than what you have now.

    This isn't your fault but I hate the dot notation in 2.0 to invoke an accessor to an object's property. Maybe that is not what's going on in your code, but that is all that makes sense if txtX is a pointed to an object.

    -Lee
     
  5. hurleyint1386 thread starter macrumors member

    Joined:
    Sep 4, 2006
    #5
    Ok, I read it, but I didn't quite understand it haha. Basically, if you were to do what I'm looking to do, how would you do it? I learn better by looking at things.
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    If txtX.text is an NSTextField, or any NSControl:
    Code:
    int x = [txtX.text intValue];
    if(x > 4) {
      x = 4;
    }
    [myLabel setIntValue:x];
    
    I think the NSInteger methods are preferred now, but you were using an int, so that's that.

    -Lee
     
  7. hurleyint1386 thread starter macrumors member

    Joined:
    Sep 4, 2006
    #7
    Hmm, still having the issue. I'm used to int because I'm coming from a C++ background.
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    Try littering your code with NSLog, and see what the code path is, and what values are what. If you want to post more code, that might help.

    -Lee
     

Share This Page