Objective-C 使用 #import 和继承
我有一个假设的 UIViewController 类,名为“foo”。 foo 继承自类 bar 和类 bar #import 的“Class A”,这是 foo 广泛使用的类。 问题是,当我在 foo 中使用类 A 的实例时,我没有收到任何编译器错误,但我确实收到警告,例如,类 A 的实例不响应特定方法。 我是否必须显式 #import ClassA.h 到类“foo”中? 即使 foo 类扩展了 bar,它已经导入了它?
希望这不会太令人困惑。 如果我需要清理任何东西,请告诉我。
I have a hypothetical UIViewController class named "foo". foo inherits from class bar and class bar #import's "Class A", a class which foo uses extensively. The problem is, when I'm using an instance of class A in foo, I don't get any compiler errors, but I do get a warning for instance, that an instance of Class A does not respond to a particular method. Do I have to explicitly #import ClassA.h into class 'foo'? even though class foo extends extends bar, which already imports it?
Hope that's not too confusing. Let me know if I need to clear anything up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您有循环依赖问题。 为了解决这个问题,是的,每个实现文件(
.m
)都需要#import
正确的头文件。 但是,如果您尝试将标头文件相互#import
,则会遇到问题。为了使用继承,您需要知道超类的大小,这意味着您需要
#import
它。 不过,对于其他事物,例如作为指针的成员变量,或者作为参数或返回其他类型的方法,您实际上不需要类定义,因此您可以使用前向引用 解决编译器错误。It sounds like you have a circular dependency issue. In order to resolve it, yes, each imlementation file (
.m
) needs to#import
the proper header file. However, if you try to have the header files#import
each other, you'll run into problems.In order to use inheritance, you need to know the size of the superclass, which means you need to
#import
it. For other things, though, such as member variables which are pointers, or methods which take as a parameter or return the other type, you don't actually need the class definition, so you can use a forward reference to resolve the compiler errors.