Detecting if NSNumber is between 0 and 255
Asked Answered
G

4

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?

Grotesque answered 21/8, 2011 at 20:52 Comment(1)
See this question for more about converting from NSNumber to int : #3556406Sparker
B
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.

Brown answered 21/8, 2011 at 20:56 Comment(0)
H
3

use intValue to get the number as an int:

[redValue intValue]
Held answered 21/8, 2011 at 20:54 Comment(0)
D
1
if (redValue.intValue >255)
{
    // it's greater than 255
}
Diandre answered 21/8, 2011 at 20:55 Comment(0)
M
1

try this

[redValue intValue] > 255
Michikomichon answered 21/8, 2011 at 20:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.