Code Error, what's wrong with this file?

Discussion in 'iOS Programming' started by logicalpath, Jun 1, 2010.

  1. logicalpath macrumors newbie

    Joined:
    Jun 1, 2010
    #1
    I have run the console/debug several times now and have tried to modify this file, however it continues to find these 2 errors. Does anyone have any idea where I went wrong with this one?

    Code:
    #import <UIKit/UIKit.h>
    #import "CalculatorBrain.h"
    
    @interface CalculatorStanfordViewController : UIViewController 
    {
    	IBOutlet UILabel *display;
    	CalculatorBrain *brain;
    	BOOL userIsInTheMiddleOfTypingANumber;
    }
    
    -(IBAction)digitPressed:(UIButton *)sender
    
    {  
    I get this error code twice right after this curly.

    /Users/Paul/Documents/LPD/Stanford Iphone Courses/CalculatorStanford/Classes/CalculatorStanfordViewController.h:21:0 /Users/Paul/Documents/LPD/Stanford Iphone Courses/CalculatorStanford/Classes/CalculatorStanfordViewController.h:21: error: expected ';' before '{' token

    Code:
    
    
    	NSString *digit = [[sender titleLabel] text];
    
    	if (userIsInTheMiddleOfTypingANumber)
    	{
    		[display setText:[[display text] stringByAppendingString:digit]];
    	}
    	else 
    	{
    		[display setText:digit];
    		userIsInTheMiddleOfTypingANumber = YES;
    	}
    
    
    -(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
    
    
    
    Below is the m. file.

    Code:
    #import "CalculatorStanfordViewController.h"
    
    @implementation CalculatorStanfordViewController
    
    -(CalculatorBrain *) brain
    {
    	if (!brain) brain = [[CalculatorBrain alloc] init];
    	return brain;
    }
    
    -(IBAction)digitPressed:(UIButton *)sender
    {
    }
    
    -(IBAction)operationPressed:(UIButton *)sender
    {
    	NSString *operation = [[sender titleLabel]text];
    	double result = [[self brain] performOperation:operation];
    	[display setText:[NSString stringWithFormat:@"%g", result]];
    }
    
    @end
    
     
  2. petron macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #2
    Could you please add the error and the line info into the post.

    BR
    /petron
     
  3. yaniv92648 macrumors member

    Joined:
    Oct 26, 2009
    #3
    What i found...

    NSString *operation = [[[sender titleLabel]text];
    in the line above u have 3 square brackets on the left and 2 square brackets on the right.

    the methods digitPressed and operationPressed has an opening brackets but not an ending brackets.
     
  4. petron macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #4
    You are right Yaniv, the compiler will not like that...

    Good work. It seems I was lazy to read the code...

    GW
    /petron
     
  5. logicalpath thread starter macrumors newbie

    Joined:
    Jun 1, 2010
    #5
    I just tried that and I'm still getting the same two errors. I just updated the original post with the error code and put it right after the curly that is giving me the error.
     
  6. yaniv92648 macrumors member

    Joined:
    Oct 26, 2009
  7. petron macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #7
    Hi,

    Can we look at the Header file ?

    Can be missign semicolon i the header file.

    GL
    /petron
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    You do not and should not ever put implementation (i.e. the code) in the interface.
     
  9. yaniv92648 macrumors member

    Joined:
    Oct 26, 2009
    #9
    problems...

    post the h file and the m file and then perhaps we'll understand..
    if i'm not mistaken, u declared in the h file an interface an u'r trying to implement methods in the h file (should be in the m file), plus u should write @implementation before...
    this will help
    http://www.otierney.net/objective-c.html
     
  10. petron macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #10
    It seems that what we see is a header file.

    It explains why compiler want to have a semicolon. while you started the implementation.

    As robbiduncan says you can not put implementation in the header file.

    GL
    /petron
     
  11. logicalpath thread starter macrumors newbie

    Joined:
    Jun 1, 2010
    #11
    I updated the original post again, now it has the m. file as well.

    I see that some are saying I included an implementation in the h. file, could you tell me what I would need to remove and replace into the m. file? This is my first real program I've ever done so I'm not sure what to do.
     
  12. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    Where you have

    Code:
    -(IBAction)digitPressed:(UIButton *)sender
    
    you need
    Code:
    -(IBAction)digitPressed:(UIButton *)sender;
    
    in the .h file (in the @interface section). No {. No code. All of that goes in the .m file (in the @implementation section). As you are making such basic, fundamental errors I suggest you stop writing code for a few minutes and read all of Apple's excellent intro to Objective-C. Note that on the iPhone/iPad you are not in a garbage collected environment. If you don't understand something in the document don't skip over it. It is critical to understand what you are doing.
     
  13. logicalpath thread starter macrumors newbie

    Joined:
    Jun 1, 2010
    #13
    Should -(IBAction) not be in the interface area(h. file)? Would I have to move all of that into the implementation area(m. file)?
     
  14. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #14
    Go and read the document I linked to. Seriously. In short that line appears in both the interface and the implementation. In the interface you are declaring the external interface signature that other objects can see. So you use the ; to terminate the definition there. In the implementation you are implementing the interface you declared so you use the {} pair to enclose the method code.

    But as I said: it's time to read rather than type.
     
  15. logicalpath thread starter macrumors newbie

    Joined:
    Jun 1, 2010
    #15
    Sorry I was at work so I am just now reading this response.

    I'll go ahead and read the link you provided, thank you for your help. I hope you don't mind if I have further questions once I complete that.

    Thx
     

Share This Page