Object-c syntax-header file Reference (@class/#import/#include)

Source: Internet
Author: User

the difference between #import and #include in objective-c

Pre-compilation Directives
OBJECTIVE-C: #import: Supported by the GCC compiler

C,c++: #include

In Objective-c, #import is used as an improved version of the #include directive. In addition, #import determine that a file can only be imported once, which allows you to have no problem with recursive inclusion.

It's up to you to decide which one to use. In general, when importing the OBJECTIVE-C header file, use the #import, #include when the C header file is included.

The benefit of #import比起 # include is that it does not cause cross-compilation

second, @class is used to do the class reference

@class is to tell the compiler that there is such a class, as to what the definition of the class is.
@class is commonly used in header files to declare an instance variable of the class, and in M files it is necessary to use #import

As an example:
In the ClassA.h
#import ClassB.h equivalent to include the entire. h header file. If there are many. m files #import ClassA.h, then these files will #import ClassB.h add unnecessary #import and waste compilation time when compiling. In large software, it is important to reduce the include in. h files.

If just @class ClassB then there is no include ClassB.h. Only need to #import in the CLASSA.M file that requires ClassB ClassB.h

So when can I use @class? If you only need to declare a CLASSB pointer in ClassA.h, you can declare it in ClassA.h
@ClassB
...
ClassB *pointer;

Object-c syntax-header file Reference (@class/#import/#include)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.