0

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

Community
  • 1
  • 1
StuartM
  • 6,743
  • 18
  • 84
  • 160
  • 1
    By the way: As of LLVM shipped with XCode 4.3 you don't have to forward declare private methods anymore. – Danilo Jul 04 '13 at 22:22
  • Does this mean the whole interface section in the implementation file is not needed and it auto picks it up as a private method if not defined in header file? – StuartM Jul 04 '13 at 22:26
  • 1
    Yes, but only Xcode 4.3 and higher – Danilo Jul 04 '13 at 22:28
  • @StuartM I may be splitting hairs, but, no, this does not mean that "the whole interface section in the implementation file is not needed". It just means you don't have to forward declare private methods. But you still define your private properties in the private class extension, though. If you don't have private properties (as would appear to be the case here), then you don't need the private class extension. But class extensions are very useful; but for private properties more than anything else. – Rob Jul 04 '13 at 22:34

2 Answers2

1

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.

Danilo
  • 3,257
  • 2
  • 19
  • 24
  • Ok, as I have this setup as an NSObject and is not as such a Model but a dumping ground for the methods associated to the Game section... I should change the second method to also be a class method as it makes no difference to the method being available elsewhere in the app. – StuartM Jul 04 '13 at 22:19
  • That works too. If you change `checkIfGameAlreadyExistsAgainst` to be a class method (`+`), then you could use `[self checkIfGameAlreadyExistsAgainst` from `createNewGameAgainst`. – Danilo Jul 04 '13 at 22:20
1

You can use the form; your problem is understanding the distinction of class methods vs instance methods.

+ (void)createNewGameAgainst:(PFUser *)user2 withCompletion:(void (^)(BOOL success))completionHandler

Is a class method (note the '+') -- you do not need an instance of MESGameModel to call this method. However, you will have no instance of MESGameModel within the definition (or body of) that class method. You cannot use the instance methods, properties, or ivars of the MESGameModel because the instance of MESGameModel is absent in a class method.

self in this scope will be a special class which you can message but responds to the class methods.

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

Declares an instance method (note the '-'). An instance of MESGameModel may respond to this message, and within that method's definition, you will have access to the instance variables, properties, instance methods, and class methods of MESGameModel.

self will be an instance of MESGameModel which you can message, and responds to the instance methods.

justin
  • 104,054
  • 14
  • 179
  • 226
  • Thanks, I accepted the first answer as this explained the same point but have voted up. You are correct. In my case this isn't as such a class as it is only an NSObject and used as a dumping ground to put all the methods that associate with a Game (section). – StuartM Jul 04 '13 at 22:28
  • @StuartM that's fair - Danilo answered before I did. i just didn't see his answer when i answered, and my post added some details. so it seems fine for the supplemental info. you're welcome – justin Jul 04 '13 at 23:38