2

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... ?

James Webster
  • 31,873
  • 11
  • 70
  • 114
ZeroArcan
  • 117
  • 7

1 Answers1

7

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];
James Webster
  • 31,873
  • 11
  • 70
  • 114
  • 4
    I believe self does have meaning in class methods. It refers to the class the method is being called on. See http://stackoverflow.com/questions/6325453/why-is-self-allowed-in-static-context-in-objective-c for details. – Tom Jefferys Sep 26 '11 at 07:59
  • 3
    @James not quite! `self` in a class method stands for the Class object, and can indeed be used. – Yuji Sep 26 '11 at 08:00
  • The question that Tom links to incorrectly refers to class methods as "static contexts" -- as the answers there point out, "static" is not a word that applies _at all_ to Objective-C class methods. You had the correct terminology in your first answer. – jscs Sep 26 '11 at 18:02
  • 1
    `self` is **not** an instance variable. It’s a hidden parameter that exists in every Objective-C method, be it an instance method or a class method. –  Sep 26 '11 at 19:38