Why is there @interface above @implementation?
Asked Answered
G

4

38

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

Gradatim answered 21/7, 2012 at 17:32 Comment(1)
A
56

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.

Allyn answered 21/7, 2012 at 17:45 Comment(0)
E
3

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.

Ecesis answered 21/7, 2012 at 17:39 Comment(2)
No, it's not a category, it's a class extension. -1.Allyn
Not very nice with the -1..The difference between categories and extensions is mostly semantic: an extension is basically an anonymous category, requiring it to be implemented in the main implementation block of the class. Thus, setting a name in the parentheses of the second "interface" block, will effectively create a category and not an extension...Ecesis
B
2

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

Bustamante answered 21/7, 2012 at 17:35 Comment(0)
M
2

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.

Musicale answered 21/7, 2012 at 17:41 Comment(4)
Is using categories useful for anything other than declaring private methods and categorising code?Gradatim
Thank you for telling me. I didn't now this was called class extension. I just thought about it as a category until now. I will fix it.Musicale
Even if there is no name, still aren't extending the class? How can we then add instance variables?Affair
You need to use the ObjC runtime. Have a look at this answer: https://mcmap.net/q/410937/-create-instance-variables-at-runtimeMusicale

© 2022 - 2024 — McMap. All rights reserved.