0

I am so confused what is @class ? What does it when i am using this directive? when to use this directive and why? I did not get any link proper that tells why,what and when to use? Please tell me with an example?

Tinku
  • 247
  • 1
  • 3
  • 13
  • Have you seen this post http://stackoverflow.com/questions/322597/class-vs-import – Vin Jun 18 '15 at 10:19

4 Answers4

1

If you say @class MyClass, the compiler knows that it may see something like:

MyClass *myObject;

It doesn't have to worry about anything other than MyClass is a valid class, and it should reserve room for a pointer to it (really, just a pointer). Thus, in your header, @class suffices 90% of the time.

However, if you ever need to create or access myObject's members, you'll need to let the compiler know what those methods are. At this point (presumably in your implementation file), you'll need to #import "MyClass.h", to tell the compiler additional information beyond just "this is a class".

Aswin
  • 91
  • 7
0

It tells the compiler that the specified class exists so that it won't complain about the use of an unknown type.

A declaration of a delegate protocol might demonstrate this:

@class SomeClass;

@protocol SomeClassProtocol <NSObject>
@optional
- (void)someClass:(SomeClass *)someClass didSomethingSpecial:(int)specialThing;
@end

@interface SomeClass: NSObject
...
@end

Now without the use of the @class keyword the compiler would complain about the use of the type SomeClass in the protocol method, as it hadn't been declared yet.

However a major gotcha is that it will only work for object references (i.e. SomeClass *) as a pointer-to-type doesn't require the compiler to know anything else about the use of the type; it would not work for subclassing, for example:

This is illegal:

@class SomeClass;

@interface SomeOtherClass: SomeClass
...
@end
Droppy
  • 9,691
  • 1
  • 20
  • 27
0

In easiest words:

You use @class to define there exists a class named like this one. The compiler don't show an error that there is no such class. You should use it to import as few files in your header file as possible (good practice). This way, you can have a property / ivar in your header file without importing the class clearly in your header (you probably want to do it in your implementation file). This way when importing this header file from another files, you won't include extra and often spare header files.

Nat
  • 12,032
  • 9
  • 56
  • 103
  • I think, i am explaining with example that there is two class A and B. A header file i.e. A.h import in B header file. and B header file i.e. B.h. import in Aheader file. To overcome this problem ,@class is used. but i dnt know what happened when when two header file import to each other. what are its disadvantage? Please explain me . – Tinku Jun 18 '15 at 11:13
  • @Tinku It decreases the amount of code seen by the compiler and avoids long chains of imports -> class A imports B, B imports C, C imports D etc -> as a result given class imports almost all files in the project. You should import only superclass and protocols, rest should be just declared via `@class`. Importing from class A class B and from Class B class A won't do any harm in terms of errors/warnings/crashes. – Nat Jun 18 '15 at 11:19
0

This is what is known as a Forward Class Declaration

It's to tell the compiler "I know about this class, so you don't need to complain".

It's useful to place in headers to increase performance with compilation time. Oh and you will get less chance of cyclical references too; so start using these if you are not already!

There is an abundance of resource available, check some out:

https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/HeaderDoc/tags/tags.html

http://nshipster.com/at-compiler-directives/

http://www.learn-cocos2d.com/2011/10/complete-list-objectivec-20-compiler-directives/#class

Oliver Atkinson
  • 7,970
  • 32
  • 43