1

Possible Duplicate:
Difference between @interface definition in .h and .m file

Obj C class files have two files .h and .m ,in which the .h holds interface definition (@interface) and .m holds its implementation (@implementation)

But i saw in some classes there is an @interface occurring in both .h and .m?

What is the need for the @interface in both files?Is there any specific reason to do so?And what are the advantages if did so?

Community
  • 1
  • 1
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • 1
    Similar post. http://stackoverflow.com/questions/3967187/difference-between-interface-definition-in-h-and-m-file – iOS Sep 04 '12 at 09:42

4 Answers4

2

The @interface macro in the .m file is usually used for private iVars and properties for limited visibility. Of course this is completely optional but is undoubtedly good practice.

Stavash
  • 14,244
  • 5
  • 52
  • 80
2

The @interface in the .h file is generally the public interface, this is the one that you would declare the inheritance in such as

   @interface theMainInterface : NSObject

note the colons : and then the super object that this @interface is inheriting from NSObject, I do believe that this can only be done in the .h file. You can also declare the @interface with a category as well such as

   @interface theMainInterface(MyNewCatergory)

So this means that you can have multiple @interfaces in one .h file like

   @interface theMainInterface : NSObject

        // What ever you want to declare can go in here. 

   @end

   @interface theMainInterface(MyNewCategory)

        // Lets declare some more in here.

   @end

Declaring these types of @interfaces in the .h file generally makes everything declared in them public.

But you can declare private @interfaces in the .m file which will do one of three things it will privately extend the selected @interface or add a new category to a selected @interface or declare a new private @interface

You can do this by adding something like this to the .m file.

  @interface theMainInterface()
      // This is used to extend theMainInterface that we set in the .h file.
      // This will use the same @implemenation
  @end

  @implemenation theMainInterface()
      // The main implementation.
  @end

  @interface theMainInterface(SomeOtherNewCategory)
      // This adds a new category to theMainInterface we will need another @implementation for this.
  @end 

  @implementation theMainInterface(SomeOtherNewCategory)
      // This is a private category added the .m file
  @end

  @interface theSecondInterface()
      // This is a whole new @interface that we have created, this is private
  @end

  @implementation theSecondInterface()
     // The private implementation to theSecondInterface
  @end

These all work the same way, the only difference is that some are private, some are public and some have categories

I am unsure if you can inherit on an @interface in the .m file.

Hope this helps.

Popeye
  • 11,839
  • 9
  • 58
  • 91
1

The @interface that appears in .m file is usually used for internal category definition. There will be a category name followed by the @interface statement in following format

@interface ClassName (CategoryName)

@end

When the category name is empty as following format, the properties and methods inside are considered as private.

@interface ClassName ()

@end

Also note that you may have a property declared as readwrite in private category and readonly in the header. The compiler will complain if both declarations are readwrite.

// .h file
@interface ClassName
@property (nonatomic, strong, readonly) id aProperty;
@end

// .m file
@interface ClassName()
@property (nonatomic, strong) id aProperty;
@end
Steve Czetty
  • 6,147
  • 9
  • 39
  • 48
Makzan
  • 356
  • 2
  • 10
0

if we want to declare some private method then we use @interface declaration in .m file .

v_1
  • 469
  • 7
  • 18