Andy Jacobs
Andy Jacobs

Reputation: 15245

Forward class properties Objective C

i am making an Mac OS X application and i'm trying to let classes know of each other

Controller creates View1 and View2

BaseView has a property of Controller

View1 and View2 extends from BaseView

here is my example

Controller Class

#import <Cocoa/Cocoa.h>
#import "View1.h"
@class View1;
@interface Controller : NSViewController
{
    View1 *_view1;
}
@end

//////

#import "Controller.h"

@implementation Controller

- (id) init
{
    self = [super init];
    if( self )
    {
        _view1 = [[View1 alloc] initWithFrame:CGRectZero];
        _view1.controller = self;
    }
    return self;
}
@end

BaseView Class

#import <Cocoa/Cocoa.h>
#import "Controller.h"
@class Controller;
@interface BaseView : NSView
{
    Controller *_controller;
}

@property (nonatomic,assign) Controller *controller;

@end

//////

#import "BaseView.h"

@implementation BaseView

@synthesize controller = _controller;

@end

View Example Class

#import "BaseView.h"

@interface View1 : BaseView

@end

//////

#import "View1.h"

@implementation View1

@end

But it gives me this error:

Controller.m:23:16: Property 'controller' cannot be found in forward class object 'View1'

what do i do wrong?

Upvotes: 0

Views: 178

Answers (2)

Herm
Herm

Reputation: 2999

When you use a forward declaration in your header file(e.g. @class View1;), you do not need to #import the header.

In your View1.h you don't declare a @class, that's where you get the error. Nevertheless I suggest you to use forward declaration in your header file and import the needed headers in your implementation file when you need the method declarations etc. - this will prevent you from a header loop, too.

your code should look like

@class BaseView; 
@interface View1 : BaseView

@end  

////// 

#import "View1.h"
#import "BaseView.h"
@implementation View1  

@end 

Upvotes: 1

Ismael
Ismael

Reputation: 3937

I think the problem might not be in your code.

Are they in the same relative folder? Is BaseView in a different location? You might have to go to your project settings and adjust your User Header Search Paths if this is the case

Also, make sure that all of your files are set up for the target you are currently running, by clicking on them on the project navigator, and in the right-hand side menu check which targets it's marked

Upvotes: 0

Related Questions