@interface in the * .m file

I just want to know if there is a difference between:

@interface AppDelegate : NSObject <NSApplicationDelegate>
@property ...
@end

in the * .h file and:

@interface AppDelegate()
@property ...
@end

@implementation AppDelegate

in the * .m file.

When do I need to prefer the first version, and when will it be better to use the second version?

+4
source share
5 answers

Everything that you can put in the .h file can be placed in the .m file. The contents of the imported .h are directly copied to .m in the early stages of compilation, so there is no syntactic difference based on the placement of the statement.

But you will notice that there is a difference in syntax between

@interface AppDelegate : NSObject <NSApplicationDelegate>

and

@interface AppDelegate()

- , ( ) . , .m( - .h ) , , .h, "" , "private" ( Objective-C ).

+4

Public Vs.

, .h , .m - (private).

, @interface .m, ( , ), @interface .h .

UIViewController segue - , @properties .h. (.m). @properties .h .

+4

:

@interface AppDelegate()
@property ...
@end

, - , , , .

- , , Objective-C.

, _, , .

@interface AppDelegate()
- (BOOL)_doCoolThing;
@end

, , .

+2

, @interface (.h) public, , .

@interface (.m) private, .

+2
source

The difference between your code is that in the first option, you use extensions (all methods inside use only @implementation below)

in .m file

@interface ClassName() {
//methods here
}
@end
@implementation ClassName
//method implementation from ClassName() and from .h file for ClassName
@end

and in the second - the implementation of the class - all methods that you declare in .h are similar to public

You can find more about the extension here.

More about classes here

Property Information - Here

+1
source

All Articles