Call a class method from within that class

prostock picture prostock · Sep 3, 2011 · Viewed 25.5k times · Source

Is there a way to call a class method from another method within the same class?

For example:

+classMethodA{
}

+classMethodB{
    //I would like to call classMethodA here
}

Answer

Jon Reid picture Jon Reid · Sep 3, 2011

In a class method, self refers to the class being messaged. So from within another class method (say classMethodB), use:

+ (void)classMethodB
{
    // ...
    [self classMethodA];
    // ...
}

From within an instance method (say instanceMethodB), use:

- (void)instanceMethodB
{
    // ...
    [[self class] classMethodA];
    // ...
}

Note that neither presumes which class you are messaging. The actual class may be a subclass.