86

Normally we use

@interface interface_name : parent_class <delegates>
{
......
}
@end 

method in .h file and in .m file we synthesis the properties of variables declared in .h file.

But in some code, this @interface.....@end method is kept in the .m file also. What does it mean? What is the difference between them?

Also give some words about getters and setters for the interface file that is defined in .m file...

starball
  • 20,030
  • 7
  • 43
  • 238

3 Answers3

65

It's common to put an additional @interface that defines a category containing private methods:

Person.h:

@interface Person
{
    NSString *_name;
}

@property(readwrite, copy) NSString *name;
-(NSString*)makeSmallTalkWith:(Person*)person;
@end

Person.m:

@interface Person () //Not specifying a name for the category makes compiler checks that these methods are implemented.

-(void)startThinkOfWhatToHaveForDinner;
@end


@implementation Person

@synthesize name = _name;

-(NSString*)makeSmallTalkWith:(Person*)person
{
    [self startThinkOfWhatToHaveForDinner];
    return @"How's your day?";
}


-(void)startThinkOfWhatToHaveForDinner
{

}

@end

The 'private category' (the proper name for a nameless category is not 'private category', it's 'class extension') .m prevents the compiler from warning that the methods are defined. However, because the @interface in the .m file is a category you can't define ivars in it.

Update 6th Aug '12: Objective-C has evolved since this answer was written:

  • ivars can be declared in a class extension (and always could be - the answer was incorrect)
  • @synthesize is not required
  • ivars can now be declared in braces at the top of @implementation:

that is,

@implementation { 
     id _ivarInImplmentation;
}
//methods
@end
rsc
  • 10,348
  • 5
  • 39
  • 36
Benedict Cohen
  • 11,912
  • 7
  • 55
  • 67
  • 4
    Small sidenote, don't actually put anything in the parentheses when you declare the private interface. Otherwise, it just creates a category, and you don't want that. `@interface Person ()` will suffice. – Itai Ferber Oct 19 '10 at 10:38
  • Thanks itaiferber, I hadn't noticed that. I've updated my answer. – Benedict Cohen Oct 19 '10 at 14:44
  • 4
    If people are interested to know more about categories.. this [page](http://macdevelopertips.com/objective-c/objective-c-categories.html) was very useful to me. – Tim Oct 25 '11 at 12:03
  • 1
    If there is nothing in the brackets then this is actually called a `class extension` not a `category` – Paul.s Aug 06 '12 at 09:35
  • For some reason, I am able to create a private method without declaring it inbetween `@interface Person ()` and `@end` in the `.m` file. For instance I am able to just write `-(void)startThinkOfWhatToHaveForDinner{}` in the implementation and get away with it. Is this something new with iOS6, or am I missing something? – giant91 Aug 13 '13 at 21:02
  • 6
    @giant91 This answer is fairly old and the compiler has improved greatly since when it was initially written. The compiler no longer needs a declaration for a method if the method body is 'visible'. This means that class continuations (`@interface className ()`) will generally will now only contain private `@property`s. – Benedict Cohen Aug 14 '13 at 10:16
  • So does this mean writing ivars in `interface` of `implementation` in `.m` is identical? – mfaani May 24 '16 at 10:10
11

The concept is that you can make your project much cleaner if you limit the .h to the public interfaces of your class, and then put private implementation details in this class extension.

when you declare variable methods or properties in ABC.h file , It means these variables properties and methods can be access outside the class

@interface Jain:NSObject
{
    NSString *_name;
}

@property(readwrite, copy) NSString *name;
-(NSString*)makeSmallTalkWith:(Person*)jain;
@end

@Interface allows you to declare private ivars, properties and methods. So anything you declare here cannot be accessed from outside this class. In general, you want to declare all ivars, properties and methods by default as private

Simply say when you declare variable methods or properties in ABC.m file , It means these variables properties and methods can not be access outside the class

@interface Jain()
    {
        NSString *_name;
    }

    @property(readwrite, copy) NSString *name;
    -(NSString*)makeSmallTalkWith:(Person*)jain;
    @end
Shubham JAin
  • 593
  • 8
  • 15
0

you can even create other classes in .m file, for instance other small classes which inherit from the class declared in .h file but having some slight different behaviour. You could use this in a factory pattern

Enrico Cupellini
  • 447
  • 7
  • 14