views:

257

answers:

2

Hi,

I have the following code:

#import <Foundation/Foundation.h>
#import "ServerRequest.h" // works even though this line is included
#import "ServerResponseRecord.h"

@protocol ServerRequestDelegate<NSObject>

-(void)request:(id)request gotResponseRecord:(ServerResponseRecord*)response;
-(void)request:(id)request gotError:(NSError*)error;

@end

It compiles and runs fine. However, if I replace the method declarations with:

-(void)request:(ServerRequest*)request gotResponseRecord:(ServerResponseRecord*)response;
-(void)request:(ServerRequest*)request gotError:(NSError*)error;

I get the unexpected syntax error "error: expected ')' before 'ServerRequest'". The only reason I can think this might be a problem is that ServerRequestDelegate.h and ServerRequest.h #import each other. However, I don't understand why the code works with the #import line with (id)request. I also don't understand why it's a syntax error.

Can someone provide a good explanation?

A: 

#import "loops" are not a problem. #import is the same as #include except that it tracks files and makes sure the preprocessor only reads them in the first time.

Usually when you get an error like that it is due to a problem in the included file. So the error is probably in ServerResponseRecord.h, thought it is probably being tripped by actually using the object declared by it. Without seeing the complete headers it is not possible to say exactly what is going on.

Louis Gerbarg
If there were a problem in ServerResponseRecord, why would my code compile and run fine if I change the type to id?
brainfsck
Because the preprocessor is a trickything and some expansions may only be triggered in certain cases but not others. If you are looking at the file in isolation it appears good assuming ServerRequest and ServerResponseRecord are have valid definitions. By forward declaring them using @class you have proved that, which means there is a latent error in one of your other headers that only triggers in some cases.
Louis Gerbarg
+3  A: 

You've already hinted at the explanation: an #import cycle.

The first thing I'd do is remove the #include and add the following line above the @protocol definition:

@class ServerRequest;

This is a forward class declaration, and can help break the import loop. Check out this SO question for more details. Apple also has a brief explanation in this guide.

Basically, #import'ing a file causes the compiler to bring the entire text of that file into the file in question, and although #import is "smarter" than #include, it doesn't mean you're immune from import errors. The @class declaration is a way to tell the compiler that a class exists without importing the header. It's appropriate to use when you only need to know about the class name, but don't care about the methods it provides. Generally, you want to use @class in the .h file and #import in the .m file, where you're actually interacting with the class.

Quinn Taylor
Yes, moving to @class declares in the header is better at any number of levels, but unless the issue was circular dependencies without an appropriate forward declaration there may still be latent bug in the header.
Louis Gerbarg
That's true, but the fact that it works with the header included if he uses `id` as the type, but not when statically typing as ` ServerRequest*` is an indicator that the header is probably okay, and the compiler only has an issue when it starts trying to ferret out information about the `ServerRequest` class.
Quinn Taylor