conorgriffin
conorgriffin

Reputation: 4339

Do I need an '@class' declaration in my AppDelegate.h file for every view controller in my project?

Please explain your answers as I've gotten away with not having to do this so far.

Thanks

Upvotes: 1

Views: 339

Answers (3)

You only import things you need, and @class is doing something similar to #import - it's letting the compiler know a type exists. So why would you tell the app delegate about a class it will never see?

The difference is:

@class will only say the class exists, nothing more.

#import tells the code what messages the class accepts or anything else the header file declares. So you use it when the code needs to actually send messages to an object.

That's why a very general pattern is to use @class in the header, and #import in the implementation file. Sometimes you do need to import in the header, again if you have to know anything more than "this class exists".

Upvotes: 2

Ken Aspeslagh
Ken Aspeslagh

Reputation: 11594

@class is the same as a class forward declaration in C++.

You could simply #import the header file of each view controller, but it's sometimes cleaner to do forward declarations (@class) in the .h file, and only do the #import in your AppDelegate's .m file.

Upvotes: 3

Ben Gottlieb
Ben Gottlieb

Reputation: 85532

Short answer: No. See this question: @class vs. #import

Upvotes: 0

Related Questions