Objective-C 允许循环依赖吗?
我正在用 Objective-C 重写 Java 库,并且遇到了一个奇怪的情况。我有两个相互导入的类。这是一个循环依赖。 Objective-C支持这种情况吗?如果没有,你建议我如何重写它?
I'm rewriting a Java library in Objective-C and I've come across a strange situation. I've got two classes that import each other. It's a circular dependency. Does Objective-C support such a situation? If not, how do you recommend I rewrite it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
导入类不是继承。 Objective-C 不允许循环继承,但它允许循环依赖。您要做的就是使用
@class
指令在彼此的标头中声明类,并让每个类的实现文件导入另一个类的标头。即:ClassA.h
ClassA.m
ClassB.h
ClassB.m
Importing a class is not inheritance. Objective-C doesn't allow circular inheritance, but it does allow circular dependencies. What you would do is declare the classes in each other's headers with the
@class
directive, and have each class's implementation file import the other one's header. To wit:ClassA.h
ClassA.m
ClassB.h
ClassB.m