Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if NSNumber is between 0 and 255

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?

like image 532
Jack Humphries Avatar asked Aug 21 '11 20:08

Jack Humphries


3 Answers

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.

like image 164
Dair Avatar answered Sep 19 '22 10:09

Dair


use intValue to get the number as an int:

[redValue intValue]
like image 27
ennuikiller Avatar answered Sep 20 '22 10:09

ennuikiller


try this

[redValue intValue] > 255
like image 33
Andiih Avatar answered Sep 21 '22 10:09

Andiih