我正在尝试检测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];
}
此外,我在“else if(redValue&gt; 255)”行中收到此警告:有针对性和整数('NSNumber *'和'int')之间的有序比较,所以我'我假设我必须将此NSNumber转换为整数?
答案 0 :(得分:27)
应该是:
if([redValue intValue] < 0) {
...
if([redValue intValue] > 255) {
...
假设它是一个int。如果没有转到“访问数字值”下的NSNumber Class Reference外观并将intValue
替换为相应的内容。
答案 1 :(得分:3)
使用intValue将数字作为int:
[redValue intValue]
答案 2 :(得分:1)
if (redValue.intValue >255)
{
// it's greater than 255
}
答案 3 :(得分:1)
试试这个
[redValue intValue] > 255