开发者

Make UILabel show "No results" instead of "nan"

开发者 https://www.devze.com 2022-12-25 08:26 出处:网络
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\", s

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
}
  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
}


You can also try to use isnan macros:

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

精彩评论

暂无评论...
验证码 换一张
取 消