17

i recently watched a tutorial where the speaker said that it makes no difference if you use:

#import "Class.h"

or:

@class Class;

And i have to say, my apps work the one way or the other. But there have to be a difference right? So thought i ask you guys.

Thanks in advance!

Gustl007
  • 245
  • 1
  • 2
  • 12
  • Possible duplicate: http://stackoverflow.com/questions/322597/class-vs-import & http://stackoverflow.com/questions/6872154/what-does-class-do-in-ios-4-development – Alladinian Oct 19 '13 at 10:35
  • `@class` purely makes the symbol known as an Objective-C class, so you can declare pointers of that type. `#import "Class.h"` copies into your file all the lines of the .h file, so all the properties and fields and methods of the class are accessible. – Hot Licks Oct 19 '13 at 11:03
  • (It's totally untrue that it "makes no difference" which you use. I'm hoping you simply misinterpreted what the tutorial said or misunderstood the context. If not, I wouldn't trust the tutorial at all.) – Hot Licks Oct 19 '13 at 11:06
  • 1
    (You most often see `@class` in .h files, to avoid having to include the referenced class's .h when only declaring pointers of that type.) – Hot Licks Oct 19 '13 at 11:08
  • https://stackoverflow.com/questions/253051/whats-the-difference-between-import-and-class-and-when-should-i-use-one-over and https://stackoverflow.com/questions/322597/class-vs-import/322627#322627 – General Grievance Aug 24 '22 at 16:52

1 Answers1

5

As per Apple documentation :

@class allows you to declare that a symbol is an Objective-c class name without the need to #import the header file that defines the class.

You would use this where you only need the class name defined for the purposes of declaring a pointer to the class or a method parameter of the class, and you do not need to access any methods, fields, or properties in the class.

It saves a minuscule amount of compile time vs the #import, and it sometimes helps avoid messy include circularity issues.

Great answer from here

for example when you creat a protocol:

@class yourCustomView;

@protocol yourCustomViewDelegate <NSObject>

@required

- (void) somethingDidInView:(UIView*)view;

@end

@interface yourCustomView : FIView
Community
  • 1
  • 1
Magyar Miklós
  • 4,182
  • 2
  • 24
  • 42