Search code examples
iosobjective-cios5methodsclass-extensions

Creating class extension for a Class's private methods


I have created a Game Model as a class. I checked out this question about creating a class extension: Best way to define private methods for a class in Objective-C

What I have is some public methods - these are open for other VCs to use in the app. I also want some private methods, that the classes public methods can make use of, but do not need to be open to the rest of the application as such.

I thought this could be accomplished by a class extension, adding an extra interface section in the implementation file but this doesn't appear to work.

Imp file:

#import "MESGameModel.h"

@interface MESGameModel ()

-(BOOL)checkIfGameAlreadyExistsAgainst:(PFUser *)opponentUser;

@end

@implementation MESGameModel

#pragma mark - Public methods
+(void)createNewGameAgainst:(PFUser *)user2 withCompletion:(void (^)(BOOL success))completionHandler{

Later on I have the declaration of the other private method:

#pragma mark - Private methods
-(BOOL)checkIfGameAlreadyExistsAgainst:(PFUser *)opponentUser {

What I am looking for is the ability to call for example [self checkIfGameAlreadyExistsAgainst...] within the public method (createNewGameAgainst).


Solution

  • Your createNewGameAgainst method is a class method (see the + in front of the method declaration). Your checkIfGameAlreadyExistsAgainst method is a instance method (see the - in front of the method declaration). To call checkIfGameAlreadyExistsAgainst from createNewGameAgainst you need to get an instance of MESGameModel. self inside of createNewGameAgainst references the class itself, not an instance of it.