【问题标题】:Implementing class: Cannot find superclass实现类:找不到超类
【发布时间】:2011-08-04 22:33:29
【问题描述】:

我有 ForceGaugeViewController 类和 ForceGaugeController 类。我正在尝试使 ForceGaugeController 类成为 ForceGaugeViewController 的子类,但我收到错误。

错误:

找不到 ForceGaugeViewController 的接口声明 ForceGaugeController 类的超类。

ForceGaugeViewController.h

#import <UIKit/UIKit.h>
#import <math.h>
#import "HardwareController.h"
#import "ForceGaugeController.h"

@interface ForceGaugeViewController : UIViewController{
}
end

ForceGaugeViewController.m

#import "ForceGaugeViewController.h"

@implementation ForceGaugeViewController

ForceGaugeController.h

#import <Foundation/Foundation.h>
#import "ForceGaugeViewController.h"
#import "FMDatabase.h"
#import "FMResultSet.h"

@class ForceGaugeViewController;

// error here
@interface ForceGaugeController : ForceGaugeViewController{
}
@end

ForceGaugeController.m

#import "ForceGaugeController.h"

【问题讨论】:

    标签: iphone objective-c superclass


    【解决方案1】:

    您不仅可以转发引用您将继承自的类或您将实现的协议。你只需要在你已经在做的头文件中导入超类,然后删除@class 声明。

    编辑:超类 ForceGaugeViewController 也不应该在头文件中包含子类 ForceGaugeViewController

    ForceGaugeController.h

    #import <Foundation/Foundation.h>
    #import "ForceGaugeViewController.h"
    #import "FMDatabase.h"
    #import "FMResultSet.h"
    
    @interface ForceGaugeController : ForceGaugeViewController{
     }
    @end
    

    【讨论】:

    • 你为什么要把#import "ForceGaugeController.h"变成ForceGaugeController.h
    • 您不能只转发声明您将继承自的类或您将实现的协议。两者都做并不违法(尽管风格不好)。
    • @Joe 谢谢。我没有注意到我在超类头文件中有子类。它奏效了。
    • 感谢您的帮助。
    【解决方案2】:

    一定要避免循环导入: 确保不要将任何头文件或实现子类文件导入到超类接口声明所在的超类文件(.h 文件)中

    【讨论】:

      【解决方案3】:

      您在 ForceGaugeViewController.h 中包含 ForceGaugeController.h 并在 ForceGaugeController.h 中包含 ForceGaugeViewController.h。循环包含会使编译器感到困惑,这可能是问题所在。

      一个类的头文件只需要包含框架(即UIKit)、子类和该类所遵循的任何协议。前向声明将适用于实例实例变量/方法参数/属性的类。

      【讨论】:

        【解决方案4】:

        删除线:

        @class ForceGaugeViewController;
        

        您已经通过 ForceGaugeController.m 文件顶部的头文件导入了 ForceGaugeViewController 类。


        编辑 1

        正如@Chris Devereux 所指出的,您的头文件中有一个循环引用,您也希望摆脱它。


        编辑 2

        不知道我是如何错过了 ForceGaugeController.h 文件中的自导入。我认为这是一个错字,但如果不是,我相信你知道你必须删除它。

        【讨论】:

          猜你喜欢
          • 2014-10-22
          • 2017-04-10
          • 2021-03-30
          • 1970-01-01
          • 2015-03-01
          • 2015-08-29
          • 2022-01-11
          • 2020-01-31
          • 1970-01-01
          相关资源
          最近更新 更多