views:

36

answers:

1

Hi friends...

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 it means? What is the difference between them?

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

Thanks in Advance

A: 

It's common to put 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 .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.

Benedict Cohen
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.
itaiferber
Thanks itaiferber, I hadn't noticed that. I've updated my answer.
Benedict Cohen