AOY
AOY

Reputation: 355

IOS expected expression error

I want to use an if-statement inside the switch but I keep getting an expected expression error. I would be grateful if anyone could help me to solve this issue.

switch ([ApplicationModel getApplicationType]) {
        case CSApplication: {}
            break;
        case RAApplication: 
        {
            if (indexPath.row == FloorLevelIndex || indexPath.row == RoomIndex) 
            {
                [cell setUserInteractionEnabled: YES];            
            }
        }
            break;
        default:
            break;
    }

Upvotes: 0

Views: 729

Answers (3)

cjrieck
cjrieck

Reputation: 994

If FloorLevelIndex and RoomIndex are types, you won't be able to compare a number to a type (hence the error). You probably want the number representation of those or define them with a NSInteger value

Upvotes: 3

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

Make sure that [ApplicationModel getApplicationType] returns the valid data type that swtich case supports.

The variable used in a switch statement can only be integers, convertable integers (byte, short, char), strings and enums

Should be like this:

switch ([ApplicationModel getApplicationType]) {
        case CSApplication: 
             break;

       case RAApplication: 
            if (indexPath.row == FloorLevelIndex || indexPath.row == RoomIndex) 
            {
                [cell setUserInteractionEnabled: YES];            
            }
            break;

       default:
            break;
    }

Upvotes: 1

quad16
quad16

Reputation: 204

I just tried this code in my Xcode and it works fine:

NSLayoutAttribute attr = NSLayoutAttributeLeft;
switch (attr) {
    case NSLayoutAttributeTop: {}
        break;
    case NSLayoutAttributeLeft: {
        if (YES) {
            NSLog(@"works");
        }
    }
        break;
    default:
        break;
}

So the structure of your switch statement is correct.

Upvotes: 0

Related Questions