2013-07-13 52 views
0

我試圖根據哪個是4個文本字段的最高intValue來更改UILabel。這裏是我的代碼,但是當我輸入和整數時,它打印的控制檯完全錯誤。例如「1」打印爲520387200從文本字段中讀取整數錯誤

這是我的代碼,關於我在做什麼錯誤的任何想法將不勝感激!謝謝!!

 

    -(IBAction) upDateCalories{ 
     NSLog(@"Update Calories has been fired"); 

     if([breakfast.text intValue] > [lunch.text intValue] && [dinner.text intValue] && [snacks.text intValue]) { 

      MostCalorificMeal.text = @"Breakfast"; 
      NSLog(@"breakfast is highest"); 

     } 


     else if([lunch.text intValue] >  [breakfast.text intValue] && [dinner.text intValue] && [snacks.text intValue]) { 

      MostCalorificMeal.text = @"Lunch"; 
      NSLog(@"Lunch is highest"); 

     }  


     else if([dinner.text intValue] > [breakfast.text intValue] && [lunch.text intValue] && [snacks.text intValue]){ 

      MostCalorificMeal.text = @"Dinner"; 
      NSLog(@"Dinner is highest"); 

     }  

    else if([snacks.text intValue] > [breakfast.text intValue] && [lunch.text intValue] && [dinner.text intValue]) { 

      MostCalorificMeal.text = @"Snacks"; 
      NSLog(@"Snacks is highest"); 

     } 

     NSLog(@"breakfast %d",breakfast); 
     NSLog(@"lunch %d",lunch); 
     NSLog(@"dinne %d",dinner); 
     NSLog(@"snacks %d",snacks); 

    } 

回答

1

您正在記錄的值是UITextField類型的對象。您不能使用%d格式說明符登錄UITextField

更改日誌本:

NSLog(@"breakfast %[email protected]",breakfast.text); 
NSLog(@"lunch %@",lunch.text); 
NSLog(@"dinner %@",dinner.text); 
NSLog(@"snacks %@",snacks.text); 

而且你if陳述是不正確的。您不能使用該語法將一個值與幾個其他值進行比較。你需要的東西是這樣的:

if ([breakfast.text intValue] > [lunch.text intValue] && [breakfast.text intValue] > [dinner.text intValue] && [breakfast.text intValue] > [snacks.text intValue]) { 
    // breakfast is the greatest 
} else if ... 

,如果你使用的一些變量,它甚至會更好:

int breakfastVal = [breakfast.text intValue]; 
int lunchVal = [lunch.text intValue]; 
int dinnerVal = [dinner.text intValue]; 
int snacksVal = [snacks.text intValue]; 

if (breakfastVal > lunchVal && breakfastVal > dinnerVal && breakfastVal > snacksVal) { 
    // breakfast is the greatest 
} else if ... 

這樣,你不堅持一遍又一遍地評估值相同。

+0

感謝球員們,我的兩個帖子都能解決我所有的問題。再次感謝!! - user2579356剛剛編輯 – user2579356