Why is there @interface above @implementation?

I am wondering why there is twice @interface. One in class.h and other in class.m. For example:

TestTableViewController.h:

#import <UIKit/UIKit.h>

@interface TestTableViewController : UITableViewController

@end

and (automatically generated) class.m i find:

#import "TestTableViewController.h"

@interface TestTableViewController ()

@end

@implementation TestTableViewController

... methods delegated from UITable delegates

@end

So my question is, what the @interface TestTableViewController () in the .m file is about. Why it is there? Do I need it?

Thanks in advance

Why doesn’t this interface implementation work?

I seem to be having a basic syntax problem with an interface implementation. Basically I have this: public interface IMarkerInterface { } public class ConcreteObject : IMarkerInterface { } public int

Why Explicit Implementation of a Interface can not be public?

I have method in Class which is implementation of Interface. When I made it Explicit implementation I got compiler error The modifier ‘public’ is not valid for this item Why it is not allowed to have

Why this generic interface implementation doesn’t complile?

Why this generic interface implementation doesn’t complile? //The type Client<T> must implement the inherited abstract method IClient.compareTo(IClient) class Client<T> implements IClient

explicit interface implementation, why explicit casting

When a class explicitly implements an interface why do you need to explicitly cast the class instance to interface in order to use implemented method? (This example is taken from here: MSDN: Explicit

Code implementation in interface

I’ve been wondering why it’s allowed to do a code implementation in an interface, when interfaces are suppossed to contain no code implementation: public interface someInterface{ String someString =

Where is the iterator interface implementation in Java 5 and above?

As Iterator is an interface with hasNext(), next() and remove() methods. But where is the implementation for all these methods in Java classes?

Why Does Xcode 4.3 Put @interface ClassName() in Implementation File?

I created a class in Xcode 4.3, and it created the header and implementation files with @interface and @implementation in the correct locations, but there is also an @interface in the implementation f

Internal Interface implementation

Straight to the problem: I have a class that implements two interfaces: public class A : Interface1, Interface2{ // Interface 1 implementation… // Interface 2 implementation… } Is there a way (wi

Why does “interface have no implementation”? – C#

I have been reading some code theory related to multiple inheritance and interfaces. It said in all the places that interface is a class without implementation. 1) What is the use of not having imple

Interface methods – implementation vs. definition

Please explain why the following isn’t valid in Java (1.7). Having an interface: interface foo { mymethod(argument arg) } where argument also is an interface: interface argument{} an implementation

Answers

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

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.

The interface in the TestTableViewController.h file is the declaration of a class extension. There are 2 round brackets that show this. The syntax is the same as for writing a category for a class. But in this case it’s used to declare some sort of private methods the author does not want to expose in the header file

A normal category interface looks like this:

@interface TestTableViewController (Your_Category_Name)
- (void)doSomething;
@end

And the corresponding implementation:

@implementation TestTableViewController (Your_Category_Name)
-(void)doSomething {
// Does something...
}
@end

In your example there is no category name specified, so it just extends the class and you can implement the method in the normal implementation.

Normally this technique is used to “hide” methods. They are not declared in the header file and are not visible if you only import the .h file.

The second @interface directive is in the implementation file (.m) — you can infer from it that it’s meant for declaring stuff that the cretator 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.