What is the difference between @class and #import

后端 未结 8 1726
再見小時候
再見小時候 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 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

提交回复
热议问题