Why is there @interface above @implementation? Why is there @interface above @implementation? xcode xcode

Why is there @interface above @implementation?


The second @interface directive is in the implementation file (.m) -- you can infer from it that it's meant for declaring stuff that the creator of the class didn't want to expose to the user of the class. This usually means private and/or internal methods and properties. Also note that there are two types of doing this. The one (which you see here) is called a "class extension" and it's denoted by an empty pair of parentheses:

@interface MyClass ()

This one is particularily important because you can use this to add additional instance variables to your class.

The second one, called a "category", is indicated by a non-empty pair of parentheses, enclosing the name of the category, like this:

@interface MyClass (CategoryName)

and it's also used to extend the class. You can't add instance variables to a class using categories, but you can have multiple categories for the same class, that's the reason why it's mainly used to extend system/framework classes for which you don't have the source code -- so a category, in this sense, is the exact opposite of the class extension.


The second "interface" defines an extension for the "TestTableViewController" class, which is not visible to someone who only imports the h file. This is the de-facto way for creating private methods in objective C.


In there you can declare private methods and properties that you only want to use in your class, but not expose to other classes.