Objective-c "About importing classes (@class and #import的区别)"

Source: Internet
Author: User

Before we analyzed the difference between #include and #import, #import不会引起交叉编译, #import determine that a file can only be imported once, so that there is no problem in recursive inclusion.

So what's the difference between #import and @class?


(1) In essence.


Both @class and #import are information that introduces classes.

The difference is that #import will contain all the information introduced into the class, including entity variables and methods.

And @class is generally used in the header file, because he only tells the compiler, declaring the name of the class, just to use this class as a type (for example, the dog class header file introduced @class Animal;, then we can just create an instance variable of type Animal, such as:   Animal *ani; )。 It is not known how the Introduction Class (Animal) is defined (which instance variables and methods are defined). So if you declare a class with @class in the header file, you'll have to #import类 again if you use the concrete method or instance variable of the class in the. m implementation.

(2) from the time of compilation. (at compile time, not at run time)

When declaring in the. h header file with #import, if there are many classes that #import the same class, or if the files refer to the same class in turn, when the header file of the referenced class is compiled, all subsequent classes that reference it need to be recompiled, which can take a lot of time.

The use of @class will not take too much time, with @class is simply to tell the compiler has this class, the class as a type to use, so that the compilation smoothly through the good. If you also need to use the instance variables and methods of the introduced class in the. m file, it would be nice to #import the class again. The compiler compiles only the header files, so #import in. M is not much related to compile time. This saves compilation time.

(3) A few information notes.

① generally if there is an inheritance relationship with #import.

② In addition, if there is a cyclic dependency, such as: a->b,b->a such interdependence, if in the header file of two files with #import separate each other, then there will be a header file recycling error, then in the header file with @class declaration will not be error

③ There is the time to customize the proxy, if you want to declare the agent in the header file, such as
You should apply #import @interface secondviewcontroller:uiviewcontroller<xxxdelegate>, otherwise you will get an error, note xxxxdelegate is a custom

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Objective-c about Importing classes (@class and #import的区别)

Related Article

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.