Detecting if NSNumber is between 0 and 255

Jack Humphries picture Jack Humphries · Aug 21, 2011 · Viewed 9.3k times · Source

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?

Answer

Dair picture Dair · Aug 21, 2011

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.