2010-09-20 94 views
0

有人可以向我解釋爲什麼我的變量被稱爲活動爲0時定義爲一個浮點數,但顯示正確的值時定義爲一個int?我有另一個漂浮物,用於幾乎相同的東西,但完美的作品!xcode定義變量問題

@implementation AppPrefsViewController 
float age; 
int activity; 

... 

-(void) updateAValues{ 
if (selectedActivityUnit == 0){ 
    activity = 50; 
    NSLog(@"activity = %d",activity); 
} 

if (selectedActivityUnit == 1){ 
    activity = 75;  
    NSLog(@"activity = %d",activity); 
} 
} 


.... 

- (void)updateLabel { 
if (selectedUnit == 0) { 
    subView.hidden = YES; 
    age = 1; 
    NSLog(@"age = %d",age); 
} 

if (selectedUnit == 1) { 
    subView.hidden = YES; 
    age = 2; 
    NSLog(@"age = %d",age); 
} 

if (selectedUnit == 2) { 
    subView.hidden = NO; 
    age = 3; 
    NSLog(@"age = %d",age); 
} 
} 

回答

3

您使用

NSLog(@"activity = %d", activity); 

要顯示自己的價值。這適用於活動是int類型,因爲%d用於顯示int。如果要顯示花車,則需要浮動變形%f,如下所示:

NSLog(@"activity = %f", activity);