开发者

) expected before ; token in if statement

开发者 https://www.devze.com 2023-03-13 00:18 出处:网络
I\'ve defined a constant in the header file #define kPortraitMode 1 #define kLandscapeMode 2 Byte:viewOrientation;//this is my ivar that\'ll keep interface orientation info

I've defined a constant in the header file

#define kPortraitMode 1
#define kLandscapeMode 2

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

In willAutorotateToInterfaceOrientation:withDuration: method i say:

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

Then in Table View Data source method tableview:height:forIndexPath method I say:

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

height=263.0;
}
else{
height=320;
开发者_JS百科}
return height;

My question is what am I doing wrong here to get this compiler error? It's just a simple expression.


You sure that the constant defined are like this

#define kPortraitMode 1
#define kLandscapeMode 2

And not like this:

#define kPortraitMode 1;
#define kLandscapeMode 2;

The error in the line comes because you have a misplaced statement terminator ; before closing the if condition. So if you have ; in the constants defined, that may be the reason for the error.


In your real code, do you happen to have a semicolon after the #define kPortraitMode? Putting a ; after a #define is a common mistake.

Also, the Byte:viewOrientation; line looks fishy.


I see a lot of errors in your code.

First of all, you are defining kPortraitMode and kLandscapeMode, when UIInterfaceOrientation(Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown) are part of an enum, so that's unnecessary.

Related to that first point. You are assuming everything other than UIInterfaceOrientationPortrait is landscape, which is not true, because there's UIInterfaceOrientationPortraitUpsideDown, although if you don't allow upside down orientation, it should be fine. Still, bad practice.

Second, you are creating your own ivar for viewOrientation, when UIViewController already has the property interfaceOrientation, so that's extra memory you are wasting, as well as processing time, by assigning the orientation when it rotates in willAutorotateToInterfaceOrientation:withDuration:.

And finally, when using enums, imo it's better using switch than using if; you write less comparison code, and it's more clear.

This is how I'd do it:

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

精彩评论

暂无评论...
验证码 换一张
取 消