Reputation: 206
I am new in iphone development and i have little bit Question. My Question is When do we use @class and #import in .h (header )file.And if your answer is @class u can create instance but can not.. use its methods and by use of #import in .h file we can access all method and variable of second class .Then My Question is if #import contains advantage then why many people used only @class in their .h file.
Please anybody have answer then reply asap.Thanks in Advance.
Upvotes: 3
Views: 1867
Reputation: 3706
First of all, you're right with your assumption. As for advantages: The @class directive is faster, since it only discloses the Name, and the Inheritance to the Namespace (e.g. the Header File). But #import loads everything, so it's slower and means more load on the system. If your Code is a library for another system, its pretty useful if the headerfiles only load the classname (@class)
For an example. You have the class A, and are importing a Headerfile B from a library. B itself wants to use C. If it imports all data in the B Headerfile, it gets bloated, because you would load it too when importing the headerfile into your class A. But it isn't necessary, that your class A knows what Class C is capable of, because only B is using it.
Upvotes: 4
Reputation: 39978
Have you ever encountered cyclic imports,I suppose you are not.
The other answers are also correct but when it comes to cyclic imports the compiler gives error and you have to use @class instead of import.
quick example
//A.h
#import "B.h"
//B.h
#import "A.h"
In this case compiler will give you the error. So you have to use @class in one of the header files to remove cyclic import. It is true that @class is faster than #import but my projects doesn't have large amount of files that it would take hours to compile it :)
Upvotes: 2
Reputation: 3061
OK, trying to be more clear, then. This is what you usually want:
Use @class
in your .h
file if the header file doesn't need access to anything in the class you're importing (i.e. it only needs to know that the class exists in order to compile).
Use #import
in your .m
file to get access to the imported class' properties and methods.
An example, where your class Foo
needs to use another class you've created, Bar
. Bar
also has a custom initializer, -initWithCustomValue:
.
MyClass.h
@class Bar
...
Bar _instanceVariable;
MyClass.m
#import "Bar.h"
...
_instanceVariable = [[Bar alloc] initWithCustomValue:1];
This would make sure that you're not exposing unnecessary code (i.e. the contents of Bar
) to other classes that might be importing MyClass.h
.
From the Apple docs:
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.
Upvotes: 1