2011-08-21 33 views
9

我試圖檢測一個NSNumber是否介於0和255之間。每當我運行應用程序,我都會收到警報視圖,即我的編號大於255,即使它不是。我沒有這個問題,0檢測是否NSNumber介於0和255之間

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]; 

} 

此外,我收到關於「否則,如果(redValue> 255)」行這樣的警告:尖和整數之間(有序的比較「的NSNumber *」和「INT '),所以我假設我必須將此NSNumber轉換爲整數?

+0

看到這個問題的更多有關的NSNumber轉換成int:http://stackoverflow.com/questions/3555906/how-to-convert-nsnumber-to-int-in- objective-c –

回答

27

應該是:

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

if([redValue intValue] > 255) { 
... 

假設它是一個int。如果在「訪問數值」下找不到NSNumber Class Reference,並用適當的東西替換intValue

+0

完美,謝謝! –

3

使用的intValue獲得數量爲int:

[redValue intValue] 
1
if (redValue.intValue >255) 
{ 
    // it's greater than 255 
} 
1

試試這個

[redValue intValue] > 255 
相關問題