@class vs. #import

后端 未结 16 1054
渐次进展
渐次进展 2020-11-21 22:42

It is to my understanding that one should use a forward-class declaration in the event ClassA needs to include a ClassB header, and ClassB needs to include a ClassA header t

16条回答
  •  孤街浪徒
    2020-11-21 23:23

    for extra info about file dependencies & #import & @class check this out:

    http://qualitycoding.org/file-dependencies/ itis good article

    summary of the article

    imports in header files:

    • #import the superclass you’re inheriting, and the protocols you’re implementing.
    • Forward-declare everything else (unless it comes from a framework with a master header).
    • Try to eliminate all other #imports.
    • Declare protocols in their own headers to reduce dependencies.
    • Too many forward declarations? You have a Large Class.

    imports in implementation files:

    • Eliminate cruft #imports that aren’t used.
    • If a method delegates to another object and returns what it gets back, try to forward-declare that object instead of #importing it.
    • If including a module forces you to include level after level of successive dependencies, you may have a set of classes that wants to become a library. Build it as a separate library with a master header, so everything can be brought in as a single prebuilt chunk.
    • Too many #imports? You have a Large Class.

提交回复
热议问题