erdemgc
erdemgc

Reputation: 125

"expected type" while implementing delegate

I am writing just a piece of code, but taking an error that "expected type @line - (void)backButtonTapped:(TopBarViewController *) topBarViewController;

What is wrong with this?

@protocol TopBarDelegate

- (void)backButtonTapped:(TopBarViewController *) topBarViewController;

@end


@interface TopBarViewController : UIViewController
{

}

@property (assign, nonatomic) id <TopBarDelegate> delegate;

-(void) backButtonPressed:(id)sender;

-(void) menuButtonPressed:(id)sender;

@end

Upvotes: 4

Views: 1303

Answers (2)

Jim
Jim

Reputation: 73936

The problem is that, when parsing that file, the compiler has no idea what TopBarViewController is. It's just a random token that it doesn't know how to process. That class is defined in a different file, so the compiler doesn't look at it while parsing this one.

You have two options:

  • Import the header file that defines that class.
  • Use @class to tell the compiler that it is a class defined elsewhere.

Generally speaking, the latter option is better, as it involves less work on the compiler's behalf and cannot lead to circular imports.

Upvotes: 3

Ahmed Mohammed
Ahmed Mohammed

Reputation: 1154

Add the following at the top. Since the protocol TopBarDelegate is defined above the class TopBarViewController, at the point you define the protocol, the compiler doesn't know there is a class called TopBarViewController. This line tells it there really is a class with that name defined somewhere.

@class TopBarViewController;

Upvotes: 11

Related Questions