9

I am trying to detect whether a NSNumber is between 0 and 255 or not. Whenever I run the app, I receive the alert view that my number is greater than 255, even when it is not. I do not have this problem with 0.

if (redValue < 0) {

    NSLog(@"Red value is less than 0");

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your number must be greater than 0." message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];
    [alert release];


} else if (redValue > 255) {

    NSLog(@"Red value is greater than 255");

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your number must be less than 255." message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];
    [alert release];

}

Additionally, I receive this warning on the "else if (redValue > 255)" line: Ordered comparison between pointed and integer ('NSNumber *' and 'int'), So I'm assuming I have to convert this NSNumber to an integer?

Jack Humphries
  • 13,056
  • 14
  • 84
  • 125
  • See this question for more about converting from NSNumber to int : http://stackoverflow.com/questions/3555906/how-to-convert-nsnumber-to-int-in-objective-c – Sam Holloway Aug 21 '11 at 20:56

4 Answers4

27

Should be:

if([redValue intValue] < 0) {
...

if([redValue intValue] > 255) {
...

Assuming it is an int. If it isn't go to the NSNumber Class Reference look under "Accessing Numeric Values" and replace intValue with the appropriate thing.

Dair
  • 15,910
  • 9
  • 62
  • 107
3

use intValue to get the number as an int:

[redValue intValue]
ennuikiller
  • 46,381
  • 14
  • 112
  • 137
1
if (redValue.intValue >255)
{
    // it's greater than 255
}
Luke
  • 11,426
  • 43
  • 60
  • 69
Arvin Am
  • 533
  • 4
  • 11
1

try this

[redValue intValue] > 255
Luke
  • 11,426
  • 43
  • 60
  • 69
Andiih
  • 12,285
  • 10
  • 57
  • 88