Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone SDK: what's the difference between #import and @class?

Tags:

iphone

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?

like image 496
sashaeve Avatar asked May 02 '10 12:05

sashaeve


People also ask

What is iPhone SDK version?

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.

What is iOS SDK used for?

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 a SDK used for?

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).


1 Answers

"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.

like image 66
TechZen Avatar answered Nov 14 '22 22:11

TechZen