• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: #import errors and @class warnings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: #import errors and @class warnings


  • Subject: Re: #import errors and @class warnings
  • From: Dave DeLong <email@hidden>
  • Date: Thu, 30 Apr 2009 10:16:17 -0600

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)

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 (email@hidden)

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:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


References: 
 >#import errors and @class warnings (From: Andre Doucette <email@hidden>)

  • Prev by Date: didReceiveMemoryWarning Confusion
  • Next by Date: Re: didReceiveMemoryWarning Confusion
  • Previous by thread: #import errors and @class warnings
  • Next by thread: Re: #import errors and @class warnings
  • Index(es):
    • Date
    • Thread