Ryan
Ryan

Reputation: 7959

Why are there two @interfaces for view controllers?

I'm learning some Objective-C, specifically iOS-related stuff. Whenever I generate a new UIViewController, or UITableViewController, etc, both the generated .h and .m files contain an @interface. The application will continue to compile if I delete the @interface from the .m file, also. What is the purpose of this @interface in the .m file?

Upvotes: 2

Views: 504

Answers (2)

Chris Trahey
Chris Trahey

Reputation: 18290

Technically, it is a "class extension" (see ("Extensions" section of) the Objective-C intro docs). In practice, it has become common to use this as a "private" interface (it is only "visible" to the file in which it exists). Objective-C does not stop anyone else from calling these methods, but they won't "see" them (think of the .h file as your public interface and this anonymous category as your private interface).

It is handy for declaring @propertys which you don't want to expose publicly.

Upvotes: 3

Julien
Julien

Reputation: 3477

The @interface in the .m file is a class extension (a category with no name). It's now in the templates to let you put any declaration of internal methods or ivars you don't want to expose to other parts of the code.

With the advent of the modern Objective-C runtime and the progress of clang, a well written class should:

1) have only public methods in the header

2) have ivars and internal methods declared in the internal class extension

Upvotes: 4

Related Questions