Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Playlist Browser with Objective-C Managing Playlist Data Passing the Playlist Object

Adham Gad
Adham Gad
9,899 Points

What is the difference between Forward Declaration and import ??

I Can't understand why we use forward declaration @Class in the header file when we need to make a property that is an instance of that class Why don't we import that class instead of forward declaring it? what is the difference ? and btw sometimes he uses forward declaration in the header file and import the class in the implementation file

2 Answers

I may be wrong but if I remember right, I believe forward declarations are mainly to avoid circular imports, where one file imports another file which imports the first file etc.... Basically when you import a file, the contents of that file are substituted at the point of import when you build your project, which is then fed to the compiler. If you have circular imports, you'd have an infinite loop which would never compile. Fortunately xcode will tell you about this before trying. The forward declaration says "Don't import this class but just know that it exists. " Without either an import or a forward declaration, you get an error that no such class exists.

Adham Gad
Adham Gad
9,899 Points

Thankss great explanation (Y)

Boon Kiat Seah
Boon Kiat Seah
66,664 Points

nicely explained thanks. You got my vote !