简体   繁体   English

Objective-C:实例方法'方法'用于不在根类中的'Class'

[英]Objective-C: Instance method 'method' is being used on 'Class' which is not in the root class

I am getting warning: Instance method 'method' is being used on 'Class' which is not in the root class. 我收到警告: 实例方法'方法'正在'类'上使用,它不在根类中。

I am calling this method (which is defined in the super class) in the static method. 我在静态方法中调用此方法(在超类中定义)。 When I execute the code, I get runtime error: unrecognized selector sent to class . 当我执行代码时,我得到运行时错误: 无法识别的选择器发送到类

Is it possible to solve this problem? 有可能解决这个问题吗? Is it possible to call super class method inside the static method? 是否可以在静态方法内调用超类方法?

Thank you 谢谢

EDIT : 编辑

Child class: 儿童班:

@interface ProfileClass : GHAsyncTestCase {}
+ (void)testGHUnitSuccess;
@end

@implementation ProfileClass
+ (void)testGHUnitSuccess {
    [self waitForStatus:kGHUnitWaitStatusSuccess timeout:10.0];
}
@end

GHAsyncTestCase is class from GHUnit framework. GHAsyncTestCase是来自GHUnit框架的类。 Maybe, it is not possible to call the super class method in static method. 也许,不可能在静态方法中调用超类方法。 If not, I will have to solve it in different way. 如果没有,我将不得不以不同的方式解决它。

SOLUTION : 解决方案

I have created shared instance of my super class and have used it in the static methods. 我已经创建了我的超类的共享实例,并在静态方法中使用了它。

Simple answer is no. 简单的答案是否定的。

To call an instance method you must have an instance of the class to send the message to. 要调用实例方法,您必须具有要向其发送消息的类的实例。 In a static method you do not have an instance of the class. 在静态方法中,您没有该类的实例。

Thus you either need to convert the called method to a static method - which should be possible if the method does not depend on any values of the instance or you have to create an instance of the class (alloc, init etc) in the static method. 因此,您需要将被调用的方法转换为静态方法-如果该方法不依赖于实例的任何值,或者在静态方法中必须创建类的实例(alloc,init等),这应该是可能的。 。

We cannot provide a more concrete way out without seeing your code. 如果没有看到您的代码,我们无法提供更具体的方法。

EDIT: After your edit it can bee seen what the issue is. 编辑:编辑后,可以看到问题是什么。
In a class method (+ (void)testGHUnitSuccess - note not a static method) self is the class which does not have the instance methods. 在类方法中(+(void)testGHUnitSuccess-注意不是静态方法),self是没有实例方法的类。 Thus as you say you need to replace self by an instance of the class - in this case a shared instance. 因此,正如您所说的,您需要用类的实例替换self-在这种情况下是共享实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM