Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6617733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:44:22+00:00 2026-05-25T20:44:22+00:00

Possible Duplicate: @class vs. #import In the .h file you can add a class

  • 0

Possible Duplicate:
@class vs. #import

In the .h file you can add a class to be seen(dont know what the correct terminolgy for this is) by using

#import "SomeClass.h"

or instead use

@class SomeClass;

I’ve tried both methods and they both worked. Whats the difference? Should I be using one of the methods and not the other? What is best practice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:44:23+00:00Added an answer on May 25, 2026 at 8:44 pm

    #import includes the content of the header in the source.
    Thus, every declaration which is in the imported header is also imported.

    @class only declares to the compiler that the given class exists, but does not import the header itself. It is called a forward declaration, as you only declares to the compiler that the class exists before defining it in details (telling which methods it implements and so on)

    Consequences:

    • When using #import in your .m file, if the header is modified, it will trigger the recompilation of the .m file that #import it on next compilation. Instead, if you use @class, your .m does not depend on the header and if the header is modified, the .m file is not recompiled.
    • Using @class also avoid cross-imports, e.g. if the class A references class B and class B references class A, then you can’t #import "A.h" in B.h and #import B.h in A.h in the same time (it would be an “import infinite loop”)
    • Using @class only declare that a class exists and does not tell the compiler which methods the class responds to.

    This is why usually the best practice is to forward-declare the class using @class A in the header (.h) files that references class A, just so that the compiler knows that “A” is a known class but doesn’t need to know more, and #import "A.h" in the implementation (.m) file so that you can call methods on the objet of class A in your source file.

    In addition to avoid import loops, this will also avoid to recompile files if they don’t need to, and thus reduce your compile time.

    The only exceptions are when the declaration of your class inherits another class, or when it declares that it conforms to a given @protocol (like delegate protocols and so on), because in this particular case, the compiler needs you to #import the whole definition of the parent class or @protocol (to know if your class correctly conforms to this given protocol).


    MyClassA.h

    // Tells the compiler that "MyClassB" is a class, that we will define later
    @class MyClassB; // no need to #import the whole class, we don't need to know the whole definition at this stage
    
    @interface MyClassA : NSObject {
        MyClassB* someB; // ok, the compiler knows that MyClassB is a class, that's all it needs to know so far
    }
    -(void)sayHello;
    -(void)makeBTalk;
    @end
    

    MyClassB.h

    @class MyClassA; // forward declaration here too
    // anyway we couldn't #import "MyClassA.h" here AND #import "MyClassB.h" in MyClassA.h as it would create an unsolvable import loop for the compiler
    @interface MyClassB : NSObject {
        MyClassA* someA; // ok, the compiler knows that MyClassA is a class, that's all it needs to know so far
    }
    -(void)talk;
    -(void)makeABePolite;
    @end
    

    MyClassA.m

    // import MyClassB so that we know the whole definition of MyClassB, including the methods it declares
    #import "MyClassB.h" // thus we here know the "-talk" method of MyClassB and we are able to call it
    
    @implementation MyClassA
    -(void)sayHello { NSLog(@"A says Hello"); }
    -(void)makeBTalk {
      [someB talk];
      // we can call the 'talk' method because we #imported the MyClassB header and knows this method exists
    }
    @end
    

    MyClassB.m

    // import MyClassA so that we know the methods it declares and can call them
    #import "MyClassA.h"
    @implementation MyClassB
    -(void)talk { NSLog(@"B is talking"); }
    -(void)makeABePolite {
      [someA sayHello];
      // we can call this because we #import MyClassA
    }
    @end
    

    PS: Note that if this is a best practice, I know a lot of developers (including myself sometimes ^^) that #import the header it needs in their .h files, instead of only forward-declare it using @class… this is some bad habit — or because these developers doesn’t know these subtleties — that you will unfortunately encounter in existing code anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Default class inheritance access I know I can set the protection level
Possible Duplicate: Why XML-Serializable class need a parameterless constructor Does anyone know if it
Possible Duplicate: from . import x using __import__ ? How does one do the
Possible Duplicate: Why won't this generic java code compile? Given the following code: import
Possible Duplicate: Class names that start with C. I am a newbie using c++
Possible Duplicate: pthread Function from a Class I am getting an error (Can not
Possible Duplicate: Why shall I use the “using” keyword to access my base class
Possible Duplicate: PHP pagination class are there any PHP/Class libraries that i can utilize
Possible Duplicate: How can a static class derive from an object? I have a
Possible Duplicate: Scala equivalent of Java java.lang.Class<T> Object Hi all, I can not call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.