Import header in object c

In Objective-c, when we use an object of one class in another class by convention, we must forward the declaration to the .h file, i.e. @class classname; . And should import the header file into the .m file, i.e. #import "header.h" . But if we import the header file into .h, we do not need to import it again into the .m file. So what is the reason for this convention? What is an effective way?

+8
ios objective-c forward-declaration
source share
5 answers

So what is the reason for this agreement?

If possible, you should offer forward declarations ( @class MONClass; ) because the compiler needs to know that typename is an objc class before using it, and also because #import can drag a ton of other headers (e.g. whole frames / libraries) by seriously expanding and complicating your dependencies and increasing build time.

What is an effective way?

Forward declarations. Your builds, rebuilds, and indexing will be much faster if you do it right.

+16
source share

You are right that importing a header in .h makes it easier (in the short term). The reason for not doing this and importing it into the implementation file (.m) is to prevent name pollution, where all the names in the imported header are available when someone imports your header. Instead, when importing your header, you need to import only your functions / classes, and the rest - when implemented

Also, if you import the title in .h, this means that every code that imported your title should be recompiled when the third-party title changes, even if nothing has changed in your title explicitly. A forward declaration avoids this problem and forces you to recompile only those implementation files (.m) that actually use a third-party header

+4
source share

Although importing files into .m makes it easier to delete several lines of code, but the general opinion that import can affect load time and response time, yes, it does and does not. Since according to the documentation an apple: -

If you are concerned that including the main header file may cause your program to bloat, do not worry. Since Mac OS X interfaces are implemented using frameworks, the code for these interfaces is in the dynamic shared library, not in your executable file. In addition, only the code used by your program is always loaded into memory at run time, so your memory footprint also remains small.

As for including a large number of header files at compile time, don't worry again. Xcode provides a precompiled header tool to speed up compilation time. By combining all the frame headers at once, there is no need to recompile the headers unless you add a new structure. In the meantime, you can use any interface from the included frameworks with a small or lower penalty for performance.

Thus, response time and download time are only affected for the first time, but in any case, link forwarding must be supported to maintain coding standards and, nevertheless, low costs :).

+2
source share

this jst to say that the compiler has a class called xx if you use @class xx;

becz u dnt needs his ritual of properties / methods now.

and in the following case, you will also need the cz property and methods, you will have to access them. what's the problem if you use @class xx in the ur.h file and don’t import xx.h. then the xx object declaration will not be generated, but an error, but access to it will generate a warning and access to proprty will generate an error

0
source share

#import is a preprocessor directive that works on a file before the compiler sees it. Whenever you get confused, conceptually think of it as copying and pasting: when you see #import foo , it means that the contents of the foo file are pasted at that point. (This is a little smarter than that, since it also protects against duplicates).

So, you are #import Foo.h in Bar.h , if there are ads in Bar.h that link to Foo.h If there is nothing in Bar.h that uses Foo, but there is Bar.m , then the import goes to Bar.m Keep ads only where they are needed.

0
source share

All Articles