如何解决预期之前; 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;
然后在表视图数据源方法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;
我的问题是我在这里弄错了什么导致此编译器错误?这只是一个简单的表达。
解决方法
您确保定义的常量是这样的
#define kPortraitMode 1
#define kLandscapeMode 2
而且不是这样的:
#define kPortraitMode 1;
#define kLandscapeMode 2;
该行中的错误是由于在关闭if
条件之前您放置了错误的语句终止符mis5ѭ。因此,如果定义的常量中有“ 5”,则可能是错误的原因。
, 在您的真实代码中,您是不是在ѭ8后面加了分号?放一个; #define之后是一个常见错误。
而且,Byte:viewOrientation;
线看起来很腥。
, 我在您的代码中看到很多错误。
首先,当UIInterfaceOrientation(Portrait,LandscapeLeft,LandscapeRight和PortraitUpsideDown)是枚举的一部分时,您要定义kPortraitMode和kLandscapeMode,因此这是不必要的。
与第一点有关。您假设除UIInterfaceOrientationPortrait之外的所有其他东西都是风景,这是不正确的,因为存在UIInterfaceOrientationPortraitUpsideDown,尽管如果您不允许上下颠倒,那应该没问题。不过,不好的做法。
其次,当UIViewController已经具有属性interfaceOrientation时,您将为viewOrientation创建自己的ivar,这样,通过在willAutorotateToInterfaceOrientation:withDuration:中旋转时分配方向,就浪费了额外的内存以及处理时间。
最后,当使用枚举时,imo使用switch比使用if更好。您只需编写较少的比较代码,就可以更加清楚。
这是我的处理方式:
double height = 0.0;
switch(self.interfaceOrientation){
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
height = 263.0;
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
height = 320;
break;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。