对象从一个类到另一个类的循环调用

发布于 2025-01-01 09:45:08 字数 108 浏览 2 评论 0原文

我有A类和B类。现在我想在ClassB中调用ClassA的方法。所以我在ClassB中导入了ClassA并创建了它的一个对象并调用了该方法。现在我想在ClassA中调用ClassB的方法。我该怎么办呢。

I have ClassA and ClassB. Now i want to call a method of ClassA in ClassB.So i imported ClassA in ClassB and created an object of it and called that method.Now i want to call method of classB in classA. How can i do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴者 2025-01-08 09:45:08

我认为你的根本问题是循环导入。答案是停止在头文件中导入。如果 ClassA 需要从 ClassB 调用某些内容,则应将 #import "ClassB.h" 添加到 ClassA.m添加到 ClassA.h。如果 ClassB 需要调用 ClassA 中的某些内容,情况也是如此。 使用前向声明该类

@class ClassB;

如果 ClassA 需要在其标头中引用 ClassB(例如 ClassB 类型的属性),那么您可以在 ClassA.h 顶部 。这告诉编译器存在一个名为 ClassB 的类,并且您稍后将告诉它该类是什么。

I assume your fundamental issue is a cyclic import. The answer to this is stop importing in your header file. If ClassA needs to call something from ClassB, then #import "ClassB.h" should be added to ClassA.m, not to ClassA.h. Same thing if ClassB needs to call something in ClassA. If ClassA needs to reference ClassB in its header (e.g. a property of type ClassB) then you can forward-declare the class using

@class ClassB;

at the top of your ClassA.h. This tells the compiler that there exists a class named ClassB, and that you will tell it what that class is at some later time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文