Declaring Methods

Discussion in 'Mac Programming' started by NSNick, Jul 8, 2008.

  1. NSNick macrumors regular

    Joined:
    Jun 27, 2008
    Location:
    Washington D.C.
    #1
    Why do I get, "fatal error: method definition not in #implementation context" when I declare the methods in AppController.h, but not when I do not declare the methods?

    #import <Cocoa/Cocoa.h>


    @interface AppController : NSObject {
    int fido;
    }

    @end

    -(int)fido;
    -(void)setFido:(int)x;


    @implementation AppController

    -(id)init
    {
    [super init];
    [self setValue:[NSNumber numberWithInt:5]
    forKey:mad:"fido"];
    NSNumber *n = [self valueForKey:mad:"fido"];
    NSLog(@"fido = %@",n);
    return self;
    }

    -(int)fido
    {
    NSLog(@"-fido is returning %d", fido);
    return fido;
    }

    -(void)setFido:(int)x
    {
    NSLog(@"-setFido: is called with %d", x);
    fido = x;
    }

    @end
     
  2. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #2
    you ended the inferface before declaring the methods

    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface AppController : NSObject {
    int fido;
    }
    
    [COLOR="blue"]-(int)fido;
    -(void)setFido:(int)x;[/COLOR]  [COLOR="SeaGreen"]//put them here[/COLOR]
    
    @end
    
    [COLOR="SeaGreen"]//not here[/COLOR]
    
    and you forgot to import AppController.h in the implementation

    Code:
    [COLOR="Blue"]#import "AppController.h"[/COLOR]
    @implementation AppController
    
    -(id)init
    {
    [super init];
    [self setValue:[NSNumber numberWithInt:5] forKey:@"fido"];
    NSNumber *n = [self valueForKey:@"fido"];
    NSLog(@"fido = %@",n);
    return self;
    }
    
    -(int)fido
    {
    NSLog(@"-fido is returning %d", fido);
    return fido;
    }
    
    -(void)setFido:(int)x
    {
    NSLog(@"-setFido: is called with %d", x);
    fido = x;
    }
    
    @end
    
     
  3. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #3
    The @end goes at the end of the .h file's contents.
     
  4. ghayenga macrumors regular

    Joined:
    Jun 18, 2008
    #4
    Because you need to have the method definitions before the @end


    @interface AppController : NSObject {
    int fido;
    }

    -(int)fido;
    -(void)setFido:(int)x;

    @end
     

Share This Page