Home  >  Q&A  >  body text

ios - #import “” (嵌入头文件) 在 .h 和 .m 文件中有什么区别?

如果在不考虑交叉引用的情况下,#import “” (嵌入头文件) 在 .h 和 .m 文件中会有什么区别?
求各位大神解答?

高洛峰高洛峰2719 days ago428

reply all(7)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:32:22

    Referring to the header file in the header file will fully know all the interface information of the imported header file during compilation. For example, if B.h is imported into A.h, if A.h is imported into SubA.h in the future, it will be imported implicitly. B.h is added, unnecessary compilation is added, and the cohesion is low;
    Import the header file in the implementation file, the compiler will only know the interface information of the class when needed, which will reduce the compilation time, and between classes The degree of coupling will be reduced

    reply
    0
  • 阿神

    阿神2017-04-17 17:32:22

    As the questioner said, if it is an interview question, just answer it like this:

    You should try to import in .m instead of .h. The reason has been well stated by other respondents. Types used in .h should be declared with @class as much as possible, and then imported in .m. There are only two situations where import must be done in .h: 1. Inherited parent class 2. Implementing a certain protocol.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:32:22

    .h is the description
    .m is the implementation
    Generally speaking, if the classes used in .h must be imported in .h
    If they are not used in .h. What is used in m can be imported in .m (or .h)

    Please refer to the coding specification for details
    Generally for convenience, what is used in .h is quoted in .h
    What is used in .m is quoted in .m

    reply
    0
  • PHPz

    PHPz2017-04-17 17:32:22

    You can import it wherever you use it
    Your .h file doesn’t need to be imported at all. Why are you importing it?

    But my approach is basically

    using @Class in .h is enough
    using #import in .m

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:32:22

    I don’t think anyone above has hit the point.

    I think the biggest difference is that writing in .m can effectively reduce cross-references. If you pay more attention, you will find that all references in Fundition and UIKit frameworks use .m and .h Using the Class declaration method has no impact on the app's runtime, but it can improve the speed of compiler preprocessing. The larger your project, the more obvious this impact will be.

    So the default template of xcode6 has removed the previous public header file .pch file in order to improve the compilation speed.

    Hope it helps.

    reply
    0
  • PHPz

    PHPz2017-04-17 17:32:22

    Objective-C language supports dynamic features, which means that you only need to provide a statement at compile time. It doesn't matter even if it is not implemented. However, at runtime, the corresponding implementation will be found when the message is actually sent. If If it is not implemented, the corresponding process will be followed.

    import .h file only introduces the header file declaration, and does not care about the implementation during compilation. Therefore, if the corresponding method is not implemented in the .m file, no error will be reported.
    import .m file is usually an implementation file. Of course, there will also be a pseudo-private API (implemented through Extension). The method selector->IMP relationship table will be found at runtime.

    Preventing cross-references is necessary because we often make repeated references.

    About sending messages dynamically, you can read: http://www.henishuo.com/runtime-message-forwarding/

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:32:22

    tip:
    Many people don’t know that using #import can prevent cross-references.
    This is also the difference between #import and #include.

    reply
    0
  • Cancelreply