Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does @class do in Objective-C?

Tags:

objective-c

People also ask

What does @() mean in Objective-C?

It's Shorthand writing. In Objective-C, any character , numeric or boolean literal prefixed with the '@' character will evaluate to a pointer to an NSNumber object (In this case), initialized with that value. C's type suffixes may be used to control the size of numeric literals.

What is a header file in Objective-C?

Header Files in Objective-C. The header file (with the file extension . h) defines the class and everything about it for the world to know. The idea is to separate the definition of a class from its nitty gritty implementation details. In this case, the header file acts as the interface to the rest of your program.

What is @property in Objective-C?

The goal of the @property directive is to configure how an object can be exposed. If you intend to use a variable inside the class and do not need to expose it to outside classes, then you do not need to define a property for it. Properties are basically the accessor methods.

What is the use of category in Objective-C?

Categories provide the ability to add functionality to an object without subclassing or changing the actual object. A handy tool, they are often used to add methods to existing classes, such as NSString or your own custom objects.


It’s a forward declaration. It essentially tells the compiler that there’s a class of that name. I use it in the interface declarations:

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

Of course you could import the header for Foo instead:

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

But if someFoo is not exposed to users of Bar, they would import an extra header file that’s of no use to them. With the @class declaration the users of Bar see no extra import, because Foo.h will be imported in the implementation file of Bar.


it is called a forward declaration.

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

this is helpful to minimize dependencies. i use them whenever i can to minimize dependencies, and significantly reduce build times.

it's the same as in c and c++:

struct mon_struct;
namespace MON { class mon_class; }

hope this will help little more

Above answer already state almost every thing , i would like to add some thing on it.

The @class Foo is a forward declaration of the Foo class. It’s like telling the compiler, foo class exists So don't bother about it right now.

Note:- Class declaration becomes very critical when both class need each other. if we do usual #import statement then, we would have an endless loop of imports and Compilers don’t like endless loops. So we use @class Classname.


@class is used for creating forward declaration of any class. If you're creating any class that's implementation is unknown to you then you can do this using @class compiler directive.

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