What is the difference between @class and #import

后端 未结 8 1696
再見小時候
再見小時候 2020-12-05 03:56

When I compile with the following code there are no errors:

@class RootViewController;
//#import \"RootViewController.h\"

When I compile wi

相关标签:
8条回答
  • 2020-12-05 03:59

    @class:- It defines that you can create instance variable of the imported class and use it in your class.

    import:- It defines that you can access the variables declared in the required imported class.

    you can use given link for more info.

    http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocDefiningClasses.html#//apple_ref/doc/uid/TP30001163-CH12-TPXREF123

    0 讨论(0)
  • 2020-12-05 04:00

    @class is a forward declaration, a good practice is to put them in the .h instead of #import for avoiding circular #import problem.

    0 讨论(0)
  • 2020-12-05 04:01

    you must have imported the this class in the class to which you want to import here. That is why you are getting error , but it can be rectify by @class example .

    0 讨论(0)
  • 2020-12-05 04:06

    @class means that the definition of the class RootViewController is not yet declared, but will be defined at run time. I believe it is like declaring an extern class in c++.

    #import is equivalent to #include.

    by the error message i could guess you just made a mistake somewhere inside RootViewController.h, such as a forgotten ; or something like that

    0 讨论(0)
  • 2020-12-05 04:09

    I decided to refer to the documentation because I was still confused:

    #import

    This directive is identical to #include, except that it makes sure that the same file is never included more than once. It’s therefore preferred and is used in place of #include in code examples throughout Objective-C–based documentation.

    This convention means that every interface file includes, indirectly, the interface files for all inherited classes. When a source module imports a class interface, it gets interfaces for the entire inheritance hierarchy that the class is built upon.

    @class

    Declarations like this simply use the class name as a type and don’t depend on any details of the class interface (its methods and instance variables), the @class directive gives the compiler sufficient forewarning of what to expect. However, where the interface to a class is actually used (instances created, messages sent), the class interface must be imported.

    http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocDefiningClasses.html#//apple_ref/doc/uid/TP30001163-CH12-TPXREF123

    0 讨论(0)
  • 2020-12-05 04:12

    Basic rule: use @class in you header file and #import in your implementation file. (However, you need to #import your class' superclass. And in some other circumstances you also need to use `#import" in the header.)

    #import is not equivalent to #include. If a file is included many times, it will be loaded each time, but with many #imports of the same file, it will still only be loaded once.

    Therefore, the main reason to use @class is not to avoid circular dependencies, but to make compilation faster.

    Here's an example of when you must use @class

    //MYControl.h
    
    @class MYControl;  // Must use class
    
    @protocol MYControlDelegate
    -(void)control:(MYControl *)control didChangeToState:(UIControlState)state;
    @end
    
    @interface MYControl : UIControl
    {
       id<MYControlDelegate> delegate_;
    }
    @property (nonatomic, assign) id<MYControlDelegate> delegate;
    @end
    
    //MYControl.m
    
    @implementation MYControl
    @synthesize delegate = delegate_;
    . . .
    

    In this case, there is nothing to import, because the delegate protocol is declared above the main class in the header file. But you still need to be able to refer to the main class which has not yet been declared. So what @class does is to just let the compiler know that there is some class that is called MYControl and will be defined at some point. (Not at runtime however. The class will be defined in the course of the compilation.)

    EDIT: From the Objective-C manual:

    Since declarations like this simply use the class name as a type and don’t depend on any details of the class interface (its methods and instance variables), the @class directive gives the compiler sufficient forewarning of what to expect. However, where the interface to a class is actually used (instances created, messages sent), the class interface must be imported. Typically, an interface file uses @class to declare classes, and the corresponding implementation file imports their interfaces (since it will need to create instances of those classes or send them messages).

    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.

    Note that circularity is mentioned in the last sentence as one in a general class of issues dealt with by using @class.

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