views:

60

answers:

2

I have a small app, where user can make some calculations and solve equations. For example, if in a square equation discriminant is less than zero, the x1 and x2 values are "nan", so when I assign x1 and x2 values to UILabels they show "nan" as well. Writing a lot of if's like

if(D<0) [label setText:[NSString stringWithFormat: @"No solutions"]];

Doesn't help-there are too many cases. I want to check if after

[label setText:[NSString stringWithFormat: @"%f", x]];

label's value is "nan", the label's value will be set to @"No solutions".

Doing simple

if(label==@"nan") {
 //code
}

doesn't help.

Thanks in advance!

+2  A: 

There are two errors in your following code block:

if(label==@"nan") {
 //code
}
  1. You are referring to label instead of [label text] (or label.text if you prefer dot notation).
  2. In Cocoa/Cocoa-Touch, you need to do string comparisons using NSString's isEqualToString method.

Instead, try:

if ([[label text] isEqualToString:@"nan"]) {
 // Code
}
Steve Harrison
Thanks! Solution was so simple
Knodel
@Mike Rychev: Glad to help!
Steve Harrison
+1  A: 

You can also try to use isnan macros:

[label setText:(isnan(x) ? @"No solutions" : [NSString stringWithFormat:@"%f", x])];
Alexander Babaev