Error Expected ";" before "{" token

Discussion in 'Mac Programming' started by aemti, Jun 17, 2010.

  1. aemti macrumors newbie

    Joined:
    Jun 17, 2010
    #1
    Can someone see where this code is wrong I can't see it but i've been staring at it for three hours! could it be in a different file? ie.) implementation files?

    Code:
    #import <UIKit/UIKit.h>
    #import "CalculatorBrain.h"
    
    @interface CalculatorViewController : UIViewController
    {
    	IBOutlet UILabel *display;
    	CalculatorBrain *brain;
    	BOOL userIsInTheMiddleOfTypingANumber;
    }
    
    - (IBAction)digitPressed:(UIButton *)sender
    {    <---this is where the error expected";"before"{" token comes in--->
    		
    	if (userIsInTheMiddleOfTypingANumber) 
    	{ 
    		[display setText:[[display text] stringByAppendingString:digit]]
    	} 
    	else 
    	{ 	
    		[display setText:digit] 
    		userIsInTheMiddleOfTypingANumber = YES 
    	}
    	NSString *digit = [[sender titleLabel] text]
    	
    }
    - (IBAction)operationPressed:(UIButton *)sender
    {
    	if (userIsInTheMiddleOfTypingANumber)
    	{ 
    		[[self brain] setOperand:[[display text] doubleValue]] 
    		userIsInTheMiddleOfTypingANumber = NO
    	}
    	NSString *operation = [[sender titleLabel] text]
    	double result = [[self brain] performOperation:operation]
    	[display setText:[NSString stringWithFormat:@"%g", result]]
    }
    
    @end
     
  2. ccamelot macrumors member

    ccamelot

    Joined:
    Feb 21, 2010
    Location:
    Poland
    #2
    You're right

    Sure, try to use *.m file as well.

    In you *.h file:

    Code:
    #import <UIKit/UIKit.h>
    #import "CalculatorBrain.h"
    
    @interface CalculatorViewController : UIViewController
    {
    IBOutlet UILabel *display;
    CalculatorBrain *brain;
    BOOL userIsInTheMiddleOfTypingANumber;
    }
    
    - (IBAction)digitPressedUIButton: (id)sender;
    - (IBAction)operationPressedUIButton: (id)sender;
    @end
    and then in *.m file put implementation


    Code:
    - (IBAction)digitPressedUIButton: (id)sender { 
    if (userIsInTheMiddleOfTypingANumber) 
     ...
     
  3. Alkiera macrumors regular

    Joined:
    Mar 11, 2008
    #3
    ccamelot is correct, it's because you're putting code inside the @interface.

    If you want to put it in one file like that, you still need to define the interface separately from the implementation.
     
  4. aemti thread starter macrumors newbie

    Joined:
    Jun 17, 2010
    #4
    could you clarify which file?

    I've tried both with no success. When you say *.m and *.h are you referring to the view controller files or the calculatorbrain.m and .h files? Thanks again!
     
  5. Ap0ks macrumors 6502

    Joined:
    Aug 12, 2008
    Location:
    Cambridge, UK
    #5
    The code ccamelot posted needs to go in your CalculatorViewController.h file, then your method implementation goes in the CalculatorViewController.m file.

    CalculatorViewController.h should look something like (as ccamelot posted):
    Code:
    #import <UIKit/UIKit.h>
    #import "CalculatorBrain.h"
    
    @interface CalculatorViewController : UIViewController
    {
    IBOutlet UILabel *display;
    CalculatorBrain *brain;
    BOOL userIsInTheMiddleOfTypingANumber;
    }
    
    - (IBAction)digitPressedUIButton: (id)sender;
    - (IBAction)operationPressedUIButton: (id)sender;
    @end
    CalculatorViewController.m should look something like:
    Code:
    #import "CalculatorViewController.h"
    
    @implementation CalculatorViewController
    
    - (IBAction)digitPressed:(UIButton *)sender
    {    <---this is where the error expected";"before"{" token comes in--->
    		
    	if (userIsInTheMiddleOfTypingANumber) 
    	{ 
    		[display setText:[[display text] stringByAppendingString:digit]]
    	} 
    	else 
    	{ 	
    		[display setText:digit] 
    		userIsInTheMiddleOfTypingANumber = YES 
    	}
    	NSString *digit = [[sender titleLabel] text]
    	
    }
    - (IBAction)operationPressed:(UIButton *)sender
    {
    	if (userIsInTheMiddleOfTypingANumber)
    	{ 
    		[[self brain] setOperand:[[display text] doubleValue]] 
    		userIsInTheMiddleOfTypingANumber = NO
    	}
    	NSString *operation = [[sender titleLabel] text]
    	double result = [[self brain] performOperation:operation]
    	[display setText:[NSString stringWithFormat:@"%g", result]]
    }
    
    @end
    
     

Share This Page