I have an NSMutableArray with 1 float value and 4 nan values. I want to execute some operations if the array object is nan. How can I write an if condition?
Asked
Active
Viewed 415 times
0
-
do you want to check that whether array contains any object or not!!!???? – NiravPatel Oct 07 '13 at 11:08
-
What do you mean by *"if the array object is none"* ? – Martin R Oct 07 '13 at 11:23
-
@MartinR Sorry, errata mistake! – Ali Sufyan Oct 07 '13 at 11:25
4 Answers
4
try isnan function
Iterate your array and put check
isnan([[Array objectAtIndex:i] floatValue])
(dont forget to add math.h library)

BaSha
- 2,356
- 3
- 21
- 38
-
-
isnan([[array objectAtIndex:i] floatValue]) worked like a charm! without math.h import. – Ali Sufyan Oct 07 '13 at 11:27
1
NSNumber *num = //your number;
float value = [num floatValue];
if (isnan(value))
{
NSLog(@"is nan");
}

Inder Kumar Rathore
- 39,458
- 17
- 135
- 184
1
Do something like this:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithFloat:1.0]];
[array addObject:[NSNull null]];
[array addObject:[NSNull null]];
[array addObject:[NSDecimalNumber notANumber]];
BOOL foundNull = NO;
for (id value in array) {
if (!value || value == [NSNull null]) {
foundNull = YES;
} else if ([value isKindOfClass:[NSDecimalNumber class]]) {
if ([value isEqualToNumber:[NSDecimalNumber notANumber]]) {
foundNull = YES;
}
}
}
NSLog(@"Found null/NaN: %i", foundNull);

Marius Kažemėkaitis
- 1,723
- 19
- 22
0
You can also use
[NSDecimalNumber notANumber] method to compare and check if this a valid number or not.

RK-
- 12,099
- 23
- 89
- 155