Make UILabel show "No results" instead of "nan"
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 "na开发者_如何学编程n", 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!
There are two errors in your following code block:
if(label==@"nan") {
//code
}
- You are referring to
label
instead of[label text]
(orlabel.text
if you prefer dot notation). - In Cocoa/Cocoa-Touch, you need to do string comparisons using
NSString
'sisEqualToString
method.
Instead, try:
if ([[label text] isEqualToString:@"nan"]) {
// Code
}
You can also try to use isnan macros:
[label setText:(isnan(x) ? @"No solutions" : [NSString stringWithFormat:@"%f", x])];
精彩评论