#import statements in .m or .h in objective-c?

I ended up getting these files in both .h and .m files, this is my first Objective-C program, so I would like some clarification so that I can clear them.

+3
source share
5 answers

If this does not affect the definition of the interface, you should put it in a .m file.

If you just use a class, use the forward declaration:

@class AClass;

@interface Bob : NSObject {
  AClass* a;
}

If you are implementing something, then import it:

#import "SomeProtocol.h"

@interface Bob : NSObject<SomeProtocol> {
}

" ", . Objective C #import , , , , .

+11

:

  • , , , class (.h) (.m).
  • , , .

MyClass.m, MyClass.h MyInclude.h:

, # 1:

// MyClass.h
@class MyInclude;

@interface MyClass : NSObject {
    MyInclude *myIncludeObj;
}

// MyClass.m
#import "MyClass.h"
#import "MyInclude.h"

Exaple, # 2:

// MyClass.h
#import "MyInclude.h"
@interface MyClass : NSObject {
    MyInclude myIncludeObj; // MyInclude could be a plain C structure
}

// MyClass.m
#import "MyClass.h"
+4

#import #include , , . # .

, . #imports (.m). (.h),

@class MyClass;

, .

, .

+1

: .h , .h. , , .h , . Xcode #import <UIKit/UIKit.h> .h, .m.

(, , ivars ), forward-declarations #import .m

: .

+1

#import .m. , , , :

@class Cocos2DController;

@interface HoppersAppDelegate : NSObject <UIApplicationDelegate> {
Cocos2DController*  controller;
}

, , . , , , #imports .

, : #import, #import, .

0

Source: https://habr.com/ru/post/1777933/


All Articles