Difference between @interface definition in .h and .m files

Usually we use

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

in the .h file and in the .m file, we synthesize the properties of the variables declared in the .h file.

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

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

Thanks at Advance

+72
header-files objective-c interface setter getter
Oct 19 '10 at 9:59
source share
3 answers

Usually, an additional @interface is placed for placement, which 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 your day?"; } -(void)startThinkOfWhatToHaveForDinner { } @end 

"Personal category" (the proper name for an unnamed category is not a "private category", it is a "class extension") .m prevents the compiler from warning that the methods are defined. However, since the @interface in the .m file is a category, you cannot define ivars in it.

August 6th update '12: Objective-C has evolved since this answer was written:

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

    @implementation {id _ivarInImplmentation; } // methods @end

+57
Oct 19 '10 at 10:35
source share

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

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

 @interface Jain:NSObject { NSString *_name; } @property(readwrite, copy) NSString *name; -(NSString*)makeSmallTalkWith:(Person*)jain; @end 

@Interface allows you to declare personal ivars, properties, and methods. So, everything that you declare here cannot be accessible from outside this class. In general, you want to declare all ivars, properties and default methods as private

Just say when you declare variable methods or properties in ABC.m file. This means that these variables cannot access outside the class.

 @interface Jain() { NSString *_name; } @property(readwrite, copy) NSString *name; -(NSString*)makeSmallTalkWith:(Person*)jain; @end 
+3
Apr 01 '17 at 16:50
source share

you can even create other classes in the .m file, for example, other small classes that inherit a class declared in the .h file but that have slightly minor behavior. You can use this in factory template.

0
Dec 09 '16 at 18:19
source share



All Articles