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 Simple iPhone App with Objective-C Views and View Controllers IBOutlet

Thomas Lahoud
Thomas Lahoud
6,092 Points

Why are outlets created in the header file instead of the implementation file?

Bit confused as to the reason why we need to place the outlet into the header file?

2 Answers

Stone Preston
Stone Preston
42,016 Points

you can create properties (including outlets) and methods in implementation files if you would like by using a class extension inside the implementation. However other classes would be unable to see these properties and methods. Its similar to public/private in other languages (though not exactly) with the interface in the header being "public", while the interface in the implementation is "private" (remember this is not 100% true, someone can still call the methods/access the properties if they know the method signature and ignore compiler warnings. there is not really a true concept of "private" in objective c. )

Some people believe its best not to expose properties to outside classes unless necessary. In my experience most people declare them in the header, but its perfectly fine to put them in the implementation if you want to keep them "hidden"

an example of a class extension inside an implementation file is below:

// start of the class extension
@interface ClassName()

- (void) someMethod;
@property (nonatomic, strong) NSArray *someArray;

@end
// end of the class extension

@implementation ClassName
....
Chris Shaw
Chris Shaw
26,676 Points

Hi Thomas,

Essentially we declare all our class properties, methods, @IBOutlet's, @IBAction's etc. within our interface as that is the facade as it's known for the class, what happens is the implementation references the interface giving it access to the declarations.

Unlike Swift where you assign all your properties and such in the same single file, you need to be explicit in Objective-C because it's an older programming language.

Hope that helps.

Stone Preston
Stone Preston
42,016 Points

you need to be explicit in Objective-C because it's an older programming language

thats not entirely true. you can put all your properties and outlets in the implementation file if you wanted using a class extension , but only that class would be able to see them them. putting them in the header exposes them to the outside