#import在目标C:我这样做错了吗? [英] #import in objective C: Am I doing this wrong?

查看:111
本文介绍了#import在目标C:我这样做错了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,找不到更合适的标题。

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 the other class. For that to work (I guess?) the other classes headers file should be imported so it knows which methods it has and such.

这里是我的代码(剥离)

Here is my code (stripped down)

MainMenuController.h:

MainMenuController.h:

    #import <Cocoa/Cocoa.h>
    #import "IRCConnection.h"

    @interface MainMenuController : NSViewController {    
        IRCConnection *ircConnection;
    }

    @property (strong) IRCConnection *ircConnection;

    @end

IRCConnection.h:

IRCConnection.h:

    #import <Foundation/Foundation.h>
    #import "MainMenuController.h"

    @interface IRCConnection : NSObject {

        MainMenuController *mainMenuController;
    }

    @property (strong) MainMenuController *mainMenuController;

    @end

正如你所看到的,这会在一个中创建一个错误(未知类型名称IRCConnection),并在另一个未知类型名称MainMenuController中创建错误。

As you can see they both import each other, but this creates an error (Unknown type name 'IRCConnection') in one, and in the other Unknown type name 'MainMenuController'.

但是当连接只是一种方式例如只有MainMenuController知道IRCConnection),因此只有一个import语句在其中之一,它工作正常。

However when the connection is just one way (e.g. only MainMenuController knows about IRCConnection) and thus there is only an import statement in one of the two, it works fine.

我如何让他们知道彼此?

How can I have them to know about each other? In both ways.

希望这个问题有意义。

推荐答案

您可以从 IRCConnection.h 中删除​​导入,而改为使用 @class 语句。

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

像这样:

#import <Foundation/Foundation.h>

@class MainMenuController;

@interface IRCConnection : NSObject {

然后添加 #importMainMenuController.h IRCConnection.m

这篇关于#import在目标C:我这样做错了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆