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 6641603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:49:48+00:00 2026-05-25T23:49:48+00:00

I have two classes ClassA and Class B (they are viewControllers). Class A is

  • 0

I have two classes ClassA and Class B (they are viewControllers).
Class A is a delegate of classB.
ClassA “laucnhes” and instance of ClassB.
ClassB call methods on classA.

Let’s say it’s :

#import "ClassB.h"

@interface ClassA : NSObject {
    ClassB* subController;
}

- (void) doThis;

-------------------------------

#import "ClassA.h"

@interface ClassB : NSObject {
    ClassA* delegate;
}

-------------------------------

@implementation ClassB

- (void) someMethod {
   AnObject* myObj = [self.delegate.arr objectAtIndex:8];
   [self.delegate doThis];
}

Doing that way, A must import B, and B must import A.

If B do not import A (with just @class A), there is a compile error for the used attribute from A.
If B imports A, there is a compile error on the ClassA* delegateline.

Why do I have those compile errors? Doesn’t #import protect again recursive calls ?

I don’t need a solution to solve that problem, I know how I may do this.
But I wonder why my #import cause those problems. These are not #includes…

  • 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-25T23:49:49+00:00Added an answer on May 25, 2026 at 11:49 pm

    Why do I have those compile errors? Doesn’t #import protect again recursive calls ?

    #import protects against repeatedly importing the same header into the same module, whether by circular includes/imports or not. It protects against that by not letting you do it: Only the first #import of a header works; subsequent #imports of the same header are ignored.

    In a circular #include situation, the preprocessor would go around the circle some number of times and then fail the build before you even get to compilation. Using #import prevents the preprocessor from getting wedged and lets the preprocessor succeed, but circular-#import code is still dodgy at best and usually will not compile.

    So, on to your specific situation.

    For the code you showed in your question, @class will work in either or both headers, and indeed you should use it in both. You’ll also need to #import both headers in both .m files.

    If B do not import A (with just @class A), there is a compile error for the used attribute from A.

    If you mean “there is a compile error at each point where I use that attribute of type ClassA *”, then yes: You can’t talk to that object because you haven’t imported its interface, so the compiler doesn’t know what messages you can send to a ClassA instance. That’s why you need to import its interface.

    If B imports A, there is a compile error on the ClassA* delegateline.

    If both headers import each other, then you have this:

    ClassA.m:
        ClassA.h
            ClassB.h
                ClassA.h (ignored because this was already imported by ClassA.m)
    ClassB.m:
        ClassB.h
            ClassA.h
                ClassB.h (ignored because this was already imported by ClassB.m)
    

    There is no way this can work without one interface preceding the other without the other interface preceding it. That’s the circle you’re running into—the circle that #import exists to break. #include allows the circle, and thereby gets wedged:

    ClassA.m:
        ClassA.h
            ClassB.h
                ClassA.h
                    ClassB.h
                        ClassA.h
                            ClassB.h
                                ClassA.h
                                    ClassB.h
                                        ClassA.h
                                            ClassB.h
                                                ClassA.h
                                                    ClassB.h
                                                        (fails at some point)
    

    Hence #import.

    So you cannot import each header from the other. Hence @class.

    But you still need to import each header from each module. That is, in fact, exactly what you need to do: Use @class in each header and use #import (on both headers) in each module.

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

Sidebar

Related Questions

I have two classes that I am testing (let's call them ClassA and ClassB).
Let's say we have these two classes: public class Base { public static int
So let's say I have two classes that inherit a base class that has
I have two classes Class A and ClassB: static class ClassA { static string
Say I have two classes in my CSS, say .classA{} and .classB{} , and
For simplicity let's say I have two classes: a class called Car and a
I have two classes (ClassA and ClassB) who both have two methods (compare and
Let's say I have the following two classes: public class TestResults { public string
I have two classes, ClassA and ClassB , as well as a many to
I have two classes, call them A and B. They both contain a Loader

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.