We can import class declaration with #import:
#import "SomeClass.h"
or declare with @class:
@class SomeClass;
What's the difference and when we should use each of them?
The iOS SDK is a software development kit that helps developers create native applications for Apple's iOS devices and platforms. The iOS SDK was formerly known as the iPhone SDK.
The iOS SDK (iOS Software Development Kit), formerly the iPhone SDK, is a software development kit (SDK) developed by Apple Inc. The kit allows for the development of mobile apps on Apple's iOS and iPadOS operating systems. Apple Inc. The iOS SDK is a free download for users of Macintosh (or Mac) personal computers.
What is an SDK? SDK stands for software development kit. Also known as a devkit, the SDK is a set of software-building tools for a specific platform, including the building blocks, debuggers and, often, a framework or group of code libraries such as a set of routines specific to an operating system (OS).
"Import" links the header file it contains. Everything in the header, including property definitions, method declarations and any imports in the header are made available. Import provides the actual definitions to the linker.
@class by contrast just tells the linker not to complain it has no definition for a class. It is a "contract" that you will provide a definition for the class at another point.
Most often you use @class to prevent a circular import i.e. ClassA refers to ClassB so it imports ClassB.h in its own ClassA.h but ClassB also refers to ClassA so it imports ClassA.h in ClassB.h. Since the import statement imports the imports of a header, this causes the linker to enter an infinite loop.
Moving the import to the implementation file (ClassA.m) prevents this but then the linker won't recognize ClassB when it occurs in ClassA.h. The @class ClassB;
directive tells the linker that you will provide the header for ClassB later before it is actually used in code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With