Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mattpryor

macrumors newbie
Original poster
Feb 11, 2010
9
0
Hi there,

Please could someone take a look at the following short bit of code and see if you can spot why I'm getting an "Expected expression before 'Location'" error when I try to compile? It's driving me nuts...

Code:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
	switch (buttonIndex) {
		case FIRST_ACTION:
			Location *location = [[self viewController] currentLocation];
			break;
		case SECOND_ACTION:
			break;
		case THIRD_ACTION:
			break;
	}
	
}

If I comment out the line after "case FIRST_ACTION:" it compiles fine.

TIA
Matt
 
With out the seeing the location class code theres not much help to be had.

Why would you say that? All of the information you need is there.

Its quite simple: you cannot perform an assignment after a label in a case statement. As the error says, its expecting an expression (rather than a declaration).

See the first answer to this answer on StackOverflow for more information:
http://stackoverflow.com/questions/1231198/

I believe this will fix it:

Code:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
	switch (buttonIndex) {
		case FIRST_ACTION: {
			Location *location = [[self viewController] currentLocation];
			break;
                }
		case SECOND_ACTION:
			break;
		case THIRD_ACTION:
			break;
	}
	
}

(warning: not tested)

(PS, I find it helps to google for error messages when you get them; I knew what the problem was but I was able to find plenty of information by simply entering "objective-c" and "expected expression").
 
The reason I stated as before is that I have code that I have written that do not use curly braces for my case statements if its only one line with a break. However after some looking it seems that when you do a declaration as the first line you need to use curly braces. Kudos to you Luke.
 
Thanks very much Luke, that works a treat. I understand now.

KoolStar: Thanks for taking a look. I should have mentioned that I had tried using another object such as NSString and got the same result.

Cheers,
Matt
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.