Msgstr "Invalid use of 'this' in a non-member function" in the context of objective-c?

Using Xcode.

In this code (func is declared in the interface), subj error is reported, standing on the line with "self".

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

What...?

+2
source share
1 answer

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

You are trying to use it in a class method +(void)... where self does not make sense. Try using a shared instance or passing an instance of the class in question to a method.

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

EDIT

My commentators noted that self makes sense in class-level contexts, but refers to the class itself. This would mean that you tried to call a method that looks like this:

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

Where you should aim:

  [myClassInstance run:action after:seconds repeat:NO]; 
+7
source

All Articles