检测NSNumber是否在0到255之间

Jac*_*ies 9 iphone objective-c nsnumber ipad ios

我试图检测NSNumber是否在0到255之间.每当我运行应用程序时,我都会收到警报视图,即我的号码大于255,即使它不是.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];

}
Run Code Online (Sandbox Code Playgroud)

另外,我在"else if(redValue> 255)"行中收到此警告:有序和整数('NSNumber*'和'int')之间的有序比较,所以我假设我必须将此NSNumber转换为整数?

Dai*_*air 27

应该:

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

if([redValue intValue] > 255) {
...
Run Code Online (Sandbox Code Playgroud)

假设它是一个int.如果没有转到"访问数字值"下的NSNumber类参考,intValue用适当的东西替换.