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

hurleyint1386

macrumors member
Original poster
Sep 4, 2006
39
12
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?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

hurleyint1386

macrumors member
Original poster
Sep 4, 2006
39
12
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
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
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

hurleyint1386

macrumors member
Original poster
Sep 4, 2006
39
12
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

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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

hurleyint1386

macrumors member
Original poster
Sep 4, 2006
39
12
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

Hmm, still having the issue. I'm used to int because I'm coming from a C++ background.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.