1

I got confused a bit about importing in Objective-C, thus I wanna make sure about something:

I have A.h and A.m, B.h and B.m, I currently added #import "B.h" in A.m, and #import "A.h" in B.h, It's working, no warning or error, but would this create any problem?

Reason I cant use @class is because I declare a protocol and an enum in A.h, and use it B.h, @class cannot catch that

Thanks for helping.

Tj3n
  • 9,837
  • 2
  • 24
  • 35
  • Possible duplicate of [Circular #import Reference](http://stackoverflow.com/questions/12994286/circular-import-reference) – Shamas S Apr 19 '17 at 08:23
  • You can use forward declaration like this. :) – Shamas S Apr 19 '17 at 08:23
  • @ShamasS I knew about `@class`, but I cant use that, since I declare a protocol and enum in A.h, and use it B.h, `@class` cannot catch that, probably its different cased than the other question – Tj3n Apr 19 '17 at 08:28
  • 1
    It never matters what you include in `.m` files. With a good class architecture you won't have problems with circular dependencies. This is very similar to avoiding retain cycles. Parent should include its children and children should not include the parent - and if they do, they need to use a forward declaration. – Sulthan Apr 19 '17 at 11:38
  • Thanks, actually, I import A.h in B.h, but i don't touch anything of class A, just because i declare a protocol in there and i want to use it, I think the safest route is to create another header file for protocol but this question is just to make sure – Tj3n Apr 20 '17 at 03:05

1 Answers1

0

#import ensures that a file is only ever included once so that you never have a problem with recursive includes. However, most decent header files protect themselves against this anyway

From What is the difference between #import and #include in Objective-C? If you are using #import it doesn't matter much in case where a file is recursively adding each other.

Varun Naharia
  • 5,318
  • 10
  • 50
  • 84