@class vs. #import

后端 未结 16 1042
渐次进展
渐次进展 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:17

    Look at the Objective-C Programming Language documentation on ADC

    Under the section on Defining a Class | Class Interface it describes why this is done:

    The @class directive minimizes the amount of code seen by the compiler and linker, and is therefore the simplest way to give a forward declaration of a class name. Being simple, it avoids potential problems that may come with importing files that import still other files. For example, if one class declares a statically typed instance variable of another class, and their two interface files import each other, neither class may compile correctly.

    I hope this helps.

    0 讨论(0)
  • 2020-11-21 23:18

    This is an example scenario, where we need @class.

    Consider if you wish to create a protocol within header file, which has a parameter with data type of the same class, then you can use @class. Please do remember that you can also declare protocols separately, this is just an example.

    // DroneSearchField.h
    
    #import <UIKit/UIKit.h>
    @class DroneSearchField;
    @protocol DroneSearchFieldDelegate<UITextFieldDelegate>
    @optional
    - (void)DroneTextFieldButtonClicked:(DroneSearchField *)textField;
    @end
    @interface DroneSearchField : UITextField
    @end
    
    0 讨论(0)
  • 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.
    0 讨论(0)
  • 2020-11-21 23:26

    Use a forward declaration in the header file if needed, and #import the header files for any classes you're using in the implementation. In other words, you always #import the files you're using in your implementation, and if you need to reference a class in your header file use a forward declaration as well.

    The exception to this is that you should #import a class or formal protocol you're inheriting from in your header file (in which case you wouldn't need to import it in the implementation).

    0 讨论(0)
  • 2020-11-21 23:26

    When I develop, I have only three things in mind that never cause me any problems.

    1. Import super classes
    2. Import parent classes (when you have children and parents)
    3. Import classes outside your project (like in frameworks and libraries)

    For all other classes (subclasses and child classes in my project self), I declare them via forward-class.

    0 讨论(0)
  • 2020-11-21 23:27

    The common practice is using @class in header files (but you still need to #import the superclass), and #import in implementation files. This will avoid any circular inclusions, and it just works.

    0 讨论(0)
提交回复
热议问题