2011-06-14 105 views
0

我在頭文件)之前預期;令牌if語句

#define kPortraitMode 1 
#define kLandscapeMode 2 

Byte:viewOrientation;//this is my ivar that'll keep interface orientation info 

在willAutorotateToInterfaceOrientation定義的常數:withDuration:方法我說:

if(toInterfaceOrientation==UIInterfaceOrientationPortrait){ 
    self.viewOrientation=kPortraitMode; 
} 
else{ 
    self.viewOrientation=kLandscapeMode;   

然後查看錶數據源的方法實現代碼如下:身高: forIndexPath方法我說:

double height; 
if (self.viewOrientation==kPortraitMode){//Here I get the error) expected before token; 

height=263.0; 
} 
else{ 
height=320; 
} 
return height; 

我的問題是我在做什麼錯在這裏得到這個編譯器錯誤?這只是一個簡單的表達。

+1

你關閉了else部分? – Aravindhan 2011-06-14 06:36:14

+0

可能不是你真正的問題,但在第二個片段中,似乎有一個失蹤的'}'。 – 2011-06-14 06:37:26

+0

第二個代碼片段是另一個範圍我的意思是另一種方法。錯誤行顯示了我上面顯示的那一行。但是如果我不使用那個有效的常量。有constatnts的特例嗎? – 2011-06-14 06:43:08

回答

6

你肯定定義的常量都是這樣

#define kPortraitMode 1 
#define kLandscapeMode 2 

,而不是像這樣:

#define kPortraitMode 1; 
#define kLandscapeMode 2; 

在該行的錯誤出現,因爲你關閉if之前有一個錯位的語句結束;條件。所以如果你在定義的常量中有;,那可能是錯誤的原因。

1

在你真實的代碼中,你碰巧在#define kPortraitMode之後有一個分號嗎?放一個; #define之後是一個常見的錯誤。

此外,Byte:viewOrientation;線看起來很腥。

1

我在代碼中看到很多錯誤。

首先,當UIInterfaceOrientation(Portrait,LandscapeLeft,LandscapeRight和PortraitUpsideDown)是枚舉的一部分時,您正在定義kPortraitMode和kLandscapeMode,因此這是不必要的。

與該第一點相關。你假設UIInterfaceOrientationPortrait以外的其他東西都是橫向的,這是不正確的,因爲UIInterfaceOrientationPortraitUpsideDown,儘管如果你不允許顛倒的方向,它應該沒問題。不過,糟糕的做法。

第二,當你在viewOrientation中創建你自己的ivar時,當UIViewController已經擁有屬性interfaceOrientation時,你會浪費額外的內存,並且通過在willAutorotateToInterfaceOrientation中旋轉時指定方向來處理時間:withDuration :.

最後,當使用枚舉時,使用開關比使用if更好;你編寫的代碼比較少,而且更清晰。

這是我會怎麼做它:

double height = 0.0; 
switch(self.interfaceOrientation){ 
    case UIInterfaceOrientationPortrait: 
    case UIInterfaceOrientationPortraitUpsideDown: 
    height = 263.0; 
    break; 
    case UIInterfaceOrientationLandscapeLeft: 
    case UIInterfaceOrientationLandscapeRight: 
    height = 320; 
    break; 
} 
+0

謝謝埃米利奧的深刻解釋。如果我不讓它顛倒,如果我不在乎風景是正確還是左撇子,這仍然是一個糟糕的做法?我真的很想找到UIViewController的屬性,它保持其方向,但不知何故,我無法找到它。但現在我看到我應該在self.view中查看它自己。 – 2011-06-14 07:01:32

+1

您應該始終儘可能地使您的代碼儘可能簡單,避免留下任何模糊的指示,這將使調試,更新和理解變得更加容易。 – EmilioPelaez 2011-06-14 07:49:06

相關問題