1

I am from Actionscript Background. In Actionscript Class Method can access only Class Methods and Class properties.

But In Objective C,

  1. How Class method gameResultAll can access Instance Method initFromPlist

    +(NSMutableArray *)gameResultAll://Class Method
    
    -(id)initFromPlist:(id)plist;//Instance Method
    
    NSMutableArray *gameResults = [GameResult gameResultAll]; // (returns GameResult array)
    
  2. Why [self init] method is called instead of [super init] to create an instance from class method.

Thanks in advance.

#import "GameResult.h"

@implementation GameResult

#define GAME_RESULT_KEY @"gameresult_key"
#define SCORE_KEY @"score"

+(NSMutableArray *)gameResultAll
{

    NSMutableArray *resultArray = [[NSMutableArray alloc] init];
    for (id plist in [[[[NSUserDefaults standardUserDefaults] dictionaryForKey:GAME_RESULT_KEY] mutableCopy] allValues]) 
    {
        GameResult *gameResult = [[GameResult alloc] initFromPlist:plist];
        [resultArray addObject:gameResult];
    }
    return  resultArray;
}

//Designated initialiser
-(id)initFromPlist:(id)plist
{

    self = [self init];
    if(self)
    {
       if([plist isKindOfClass:[NSDictionary class]])
       {
           NSDictionary *resultDictionary = (NSDictionary*)plist;
           _score = (int)resultDictionary[SCORE_KEY];
       }
    }
    return  self;
}
2
  • @H2CO3 lololol I was just going to say that Commented Jul 19, 2013 at 6:16
  • sorry guys, i am using first time. I updated. Commented Jul 19, 2013 at 6:22

2 Answers 2

2

You asked:

How Class method gameResultAll can access Instance Method initFromPlist

It can access that method because you used the alloc method, which creates an instance of GameResult. Now that you have an instance, you can use instance methods in conjunction with this instance.

By the way, this is a very common pattern, a "convenience" class method that allocates an instance of an object (with alloc) and initializes the object (with init or some permutation of that). Or, as in this case, it can create an array of these objects.

You then go on to ask:

Why [self init] method is called instead of [super init] to create an instance from class method.

I can understand the confusion, but there is an important, yet subtle distinction in the behavior of these two.

Imagine this scenario:

  • At some future date, you subclass GameResult, e.g. ArcadeGameResult;

  • You implemented an init method for ArcadeGameResult that initializes some properties unique to this subclass; and

  • You happen to initialize a ArcadeGameResult instance like so:

    ArcadeGameResult *agr = [[ArcadeGameResult alloc] initFromPlist:plist];
    

Because the initFromPlist uses [self init], it means that the the initFromPlist method of GameResult will end up calling the init method of the object (which in this example, is actually a ArcadeGameResult object). But if initFromPlist in GameResult called [super init] instead, it would not have called ArcadeGameResult's init method and thus initFromPlist would be problematic if ever used in conjunction with a subclass.

Bottom line, unless the method you're calling is the exact same method signature, it's safer to call the self rendition rather than the super rendition. It's a little more flexible in case you ever decide to subclass in the future.


There is a corollary to the counsel. When calling class methods from an instance method, you should refer to [self class] rather than the class name. So, imagine your GameResult class had a class method:

+ (void)someClassMethod
{
    // do something
}

If you had some GameResult instance method that was going to avail itself of this method, you might be tempted to write:

- (void)someInstanceMethod
{
    // do some stuff

    [GameResult someClassMethod];
}

But that's not a good idea. You would instead use the following:

- (void)someInstanceMethod
{
    // do some stuff

    [[self class] someClassMethod];
}

They look very similar, but the latter lets you implement a someClassMethod in a subclass, and this instance method will still work. If you use the former construct, the subclassed class method wouldn't be called by someInstanceMethod.

These are subtle issues, and probably not critical for your current code sample. But hopefully it illuminates the choice of [self init] versus [super init] in this situation.

Sign up to request clarification or add additional context in comments.

Comments

2

In Actionscript Class Method can access only Class Methods and Class properties.

That's not different in Objective-C either (because nothing else would make sense), so:

How Class method GameResultAll can access Instance Method initFromPlist

Only through a valid instance.

Why [self init] method is called instead of [self super] to create an instance from class method.

Because the latter is a syntax error, perhaps? Read a basic Objective-C tutorial.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.