#import in objective C: Am I doing this wrong?

前端 未结 3 1921
孤独总比滥情好
孤独总比滥情好 2021-01-21 07:48

Sorry, couldn\'t find a more appropriate title.

In My code I have two classes which should know of each others existence. So I use an instance variable which points to t

相关标签:
3条回答
  • 2021-01-21 08:05

    In the header, use forward declaration:

    @class IRCConnection;
    
    @interface MainMenuController : NSViewController {    
        IRCConnection *ircConnection; // ok
    }
    

    In the source file (.m), do #import.

    0 讨论(0)
  • 2021-01-21 08:12

    you could remove the import from IRCConnection.h and use a @class statement instead.

    like this:

    #import <Foundation/Foundation.h>
    
    @class MainMenuController;
    
    @interface IRCConnection : NSObject {
    

    then add a #import "MainMenuController.h" to IRCConnection.m

    0 讨论(0)
  • 2021-01-21 08:18

    You cannot have circular imports. You need to break them up, or introduce some forward declarations.

    0 讨论(0)
提交回复
热议问题