)之前的预期; if语句中的标记

时间:2011-06-14 06:33:06

标签: objective-c if-statement

我在头文件中定义了一个常量

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

然后在Table View数据源方法tableview:height:forIndexPath方法我说:

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

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

我的问题是我在这里做错了什么来获得这个编译错误?这只是一个简单的表达。

3 个答案:

答案 0 :(得分:6)

你确定定义的常量是这样的

#define kPortraitMode 1
#define kLandscapeMode 2

而不是这样:

#define kPortraitMode 1;
#define kLandscapeMode 2;

该行中的错误是因为在关闭;条件之前您有错误的语句终结符if。因此,如果定义的常量中有;,则可能是错误的原因。

答案 1 :(得分:1)

在您的真实代码中,您是否碰巧在#define kPortraitMode之后有分号?放一个;在#define之后是一个常见的错误。

此外,Byte:viewOrientation;行看起来很可疑。

答案 2 :(得分:1)

我发现代码中存在很多错误。

首先,当UIInterfaceOrientation(Portrait,LandscapeLeft,LandscapeRight和PortraitUpsideDown)是枚举的一部分时,您正在定义kPortraitMode和kLandscapeMode,因此这是不必要的。

与第一点相关。你假设UIInterfaceOrientationPortrait以外的所有东西都是横向的,这不是真的,因为有UIInterfaceOrientationPortraitUpsideDown,虽然如果你不允许倒置方向,它应该没问题。不好的做法。

其次,你正在为viewOrientation创建自己的ivar,当UIViewController已经具有属性interfaceOrientation时,通过在willAutorotateToInterfaceOrientation:withDuration:。中旋转时指定方向,浪费额外的内存以及处理时间。 / p>

最后,当使用枚举时,使用switch比使用if更好。你写较少的比较代码,而且更清楚。

我就是这样做的:

double height = 0.0;
switch(self.interfaceOrientation){
  case UIInterfaceOrientationPortrait:
  case UIInterfaceOrientationPortraitUpsideDown:
    height = 263.0;
    break;
  case UIInterfaceOrientationLandscapeLeft:
  case UIInterfaceOrientationLandscapeRight:
    height = 320;
    break;
}
相关问题