“Invalid use of 'this' in non-member function” in objective-c context?

拜拜、爱过 提交于 2019-11-28 02:01:17

问题


Using Xcode.

In this code (func is declared in interface), tells subj error, standing on string with 'self'.

+ (void) run: (Action) action after: (int) seconds 
{
    [self run:action after:seconds repeat:NO];
}

What the... ?


回答1:


self is an instance variable used to refer to an instance of the current object.

You are attempting to use it in a class level method +(void)... where self has no meaning. Try using a shared instance, or passing an instance of the class in question to the method.

+ (void) run:(Action)action on:(MyClass*) instance after:(int) seconds
{ 
    [instance run:action after:seconds repeat:NO];
}

EDIT

My commenters have pointed out that self does have meaning in class level contexts, but it refers to the class itself. That would mean you were trying to call a method that looks like this:

 [MyClass run:action after:seconds repeat:NO];

Where you should be aiming for:

 [myClassInstance run:action after:seconds repeat:NO];


来源:https://stackoverflow.com/questions/7552037/invalid-use-of-this-in-non-member-function-in-objective-c-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!