【问题标题】:No visible @interface for 'class' declares the select 'method' when calling super调用 super 时没有可见的“类”@interface 声明选择“方法”
【发布时间】:2014-02-10 17:31:59
【问题描述】:

我有以下代码,

.h 文件中:

@interface MyBaseClass : NSObject
{
    - (void)myMethod;
}

.m 文件中:

@implementation MyClass2000 : MyBaseClass
{
    - (void) myMethod
    {
        //Does something interesting here
        myData[0] = 0;
        myData[1] = 1;
    }
}

后来我创建了一个扩展“MyClass2000”的类,名为“MyClass2001”

.h文件:

#import "MyClass2000.h"

@interface MyClass2001 : MyClass2000
@end

.m文件:

@implementation MyClass2001 : MyClass2000
{
}

这个类与“MyClass2000”完全相同(但我特别需要将它们保存在单独的类中,并且一个扩展另一个类。我还需要一个“MyClass2002 类”,在这种情况下只需要更新 1的索引,所以我尝试如下调用[super myMethod]

下一个类扩展“MyClass2001”

.h文件:

#import "MyClass2001.h"

@interface MyClass2002 : MyClass2001
@end

.m文件:

@implementation MyClass2002 : MyClass2001
{
    - (void) myMethod
    {
        [super myMethod];

        //Only update one value
        myData[1] = 1;
    }
}

在那条线上我得到:No visible @interface for 'MyClass2001' declares the selector 'myMethod'

关于如何让它工作的任何提示?

谢谢!

【问题讨论】:

  • 您几乎可以肯定没有在 .h 文件中适当地声明您的类和方法,并根据需要将它们包含在 .m 文件中。 (您没有向我们展示您的任何 @interface 声明。)

标签: ios objective-c ios6 xcode4.5


【解决方案1】:

这不是在 Objective-C 中声明方法的方式。在您的界面中,您可以像这样声明方法:

@interface MyClass2000 : MyBaseClass {
    NSString *someVariableDeclaration;
}

- (void) myMethod;

@end

然后你像这样在你的 .m 中实现这个函数:

@implementation MyClass2000 {
    NSString * someDifferentVariableDeclaration;
}
 - (void) myMethod
{
    //Does something interesting here
    myData[0] = 0;  
    myData[1] = 1;
}
@end

【讨论】:

  • 嗨!输入我的示例代码时出现错误。更新了原帖。
  • @sandy 为什么要将实现的内容包装在 {braces} 中?
  • @sandy 您只能在大括号内进行声明。请参阅我的答案的更新。您的实现必须在 @implementation@end 内,而不是在大括号内。
  • 嗯,它的工作!我是一名 Android 开发人员,我讨厌在 Objective-C 上“摇摆不定”的感觉。希望我有更多的时间来真正掌握它。谢谢!
  • 我也可以在@implementation 之后没有任何大括号吗?
【解决方案2】:

你的线路在哪里:

[super MyClass2001];

相反,你想拥有:

[super myMethod];

您试图调用名为 MyClass2001 的方法,但该方法不存在。

【讨论】:

    【解决方案3】:

    1.检查继承层次结构。在 Objc 中,每个接口都需要实现。但是您的代码告诉您没有 MyBaseClass 实现。

    2.方法声明不能在.h中的{}内完成。

    3.方法定义不能在.m中的{}内完成

    4.实现不扩展只扩展接口。

    如果保持适当的层次结构,它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-27
      相关资源
      最近更新 更多