On 30 Apr 2009, at 17:16, Dave DeLong wrote:

Hi Andre,

#import means that the compiler will only include the file once, thus eliminating re-declaration errors. It does not, however, eliminate the problems introduced by circular dependencies (which is what you've got going here).

As a general rule, the only thing you should be #importing in a .h file are header files from classes that are not from your application or framework. (Except protocols, which I don't know of a way to forward declare)

@protocol MyProtocol ;



Matt Gallagher discussed this recently on his excellent Cocoa With Love blog: http://cocoawithlove.com/2009/04/8-confusing-objective-c-warnings-and.html

Cheers,

Dave

On Apr 30, 2009, at 12:55 AM, Andre Doucette wrote:

Hi everyone!

I have noticed a problem in a few projects and don't understand why. I have found a work around, but it seems both unnecessary and a pain due to warnings.

For one example, I have two classes, AppController and NetworkController.

For the AppController class:
---------------------------------------------------------
#import <Foundation/Foundation.h>
#import "NetworkController.h"
@interface AppController : NSObject {
        NetworkController *networkController;
}

@implementation AppController
- (void)awakeFromNib {
networkController = [[NetworkController alloc] initWithAppController:self];
}
@end

And for the NetworkController class:
---------------------------------------------------------
#import <Foundation/Foundation.h>
#import "AppController.h"
@interface NetworkController : NSObject {
        AppController *appController;
}
- (id)initWithAppController:(AppController *)inAppController;
@end

@implementation NetworkController
- (id)initWithAppController:(AppController *)inAppController {
        self = [super init];
        appController = inAppController;
        return self;
}
@end

I want the two controllers to "know" about each other. The AppController object is created in the NIB file, and in it's awakeFromNib method, I create the NetworkController object, passing in a reference to itself.

When trying to compile this, I get a series of errors. In my AppController.m file, I get "error: syntax error before AppController" and "warning: '@end' must appear in an @implementation context". In my NetworkController.m, I get "error: syntax error before 'NetworkController'".

It seems that it doesn't like the double #import, but I thought the whole idea behind #import was that it ensured one-time includes. If I take either #import "NetworkController.h" or #import "AppController.h" and change them to forward declarations (that is, @class NetworkController; or @class AppController;), this works, but then I get a sprinkling of errors everywhere saying that methods may not be implemented.

Any thoughts?
Thanks!
Andre
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/davedelong%40me.com

This email sent to davedel...@me.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/adc%40jeremyp.net

This email sent to a...@jeremyp.net

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to