6

What is the difference when using @class or #import in objective-c?

I have seen various tutorials and only a few use @class while most of the others use #import.

some_id
  • 29,466
  • 62
  • 182
  • 304
  • possible duplicate of [Objective-C @class vs. #import](http://stackoverflow.com/questions/322597/objective-c-class-vs-import) – kubi Jul 12 '10 at 13:04

2 Answers2

13

@class doesn't import the file, it just says to the compiler "This class exists even though you don't know about it, don't warn me if I use it". #import actually imports the file so you can use all the methods and instance variables. @class is used to save time compiling (importing the whole file makes the compile take more time). You can use #import if you want, it will just take longer for your project to build.

nevan king
  • 112,709
  • 45
  • 203
  • 241
  • 2
    Sometimes, particularly in the case of circular references, you absolutely cannot use #import. @class is a forward declaration of a class, and since it doesn't define the class, can break circular references. – Jay O'Conor Jul 12 '10 at 17:57
  • I thought that #import protected against those, by only adding each file one time. – nevan king Jul 12 '10 at 18:26
6

See Defining a Class, especially Referring to Other Classes.