What does @class do in Objective-C?

Can someone explain to me what the @class declaration does in an objective way and what are the cases in which we should use this declaration.

+43
objective-c
Oct 11 2018-10-10T00:
source share
4 answers

Own declaration. Essentially, it tells the compiler that theres a class of that name. I use it in interface declarations:

@class Foo; @interface Bar : NSObject { Foo *someFoo; } @end 

Of course, you can import the header instead of Foo :

 #import "Foo.h" @interface Bar : NSObject { Foo *someFoo; } @end 

But if someFoo not provided to Bar users, they will import an additional header file that they do not need. When declaring @class , Bar users do not see additional import because Foo.h will be imported into the Bar implementation file.

+81
Oct 11 '10 at 8:48
source share

it is called forward announcement.

you use this directive to tell the compiler that there is an objc class with the specified name. your other options include an interface or using an identifier for variables or types.

this is useful to minimize dependencies. I use them when I can to minimize dependencies and significantly reduce build time.

this is the same as in c and c ++:

 struct mon_struct; namespace MON { class mon_class; } 
+23
Oct 11 '10 at 8:52
source share

hope this helps a little more

The answer already says almost everything, I would like to add something to it.

@class Foo is a direct declaration of the Foo class. It looks like a compiler, the foo class exists. Do not worry about it right now.

Note. - Class declaration becomes very important when both classes need each other. if we do the usual #import operator, then we will have an infinite import loop, and compilers do not like infinite loops. Therefore, we use @class Classname .

+8
Aug 22 '14 at 7:15
source share

@class used to create a direct declaration of any class. If you create any class whose implementation is unknown to you, you can do this using the @class directive.

You can implement this class when the implementation is known to you.

+1
Aug 07 2018-12-12T00:
source share



All Articles