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 trialAdham Gad
9,899 PointsWhat 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
James Pamplona
10,015 PointsI 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.
Boon Kiat Seah
66,664 Pointsnicely explained thanks. You got my vote !
Adham Gad
9,899 PointsAdham Gad
9,899 PointsThankss great explanation (Y)