Quick syntax problem

Discussion in 'iOS Programming' started by mattpryor, Mar 4, 2010.

  1. mattpryor macrumors newbie

    Joined:
    Feb 11, 2010
    #1
    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
     
  2. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #2
    With out the seeing the location class code theres not much help to be had.
     
  3. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #3
    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").
     
  4. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #4
    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.
     
  5. mattpryor thread starter macrumors newbie

    Joined:
    Feb 11, 2010
    #5
    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
     

Share This Page