Accessing class method in objective c. Using self or classname? Accessing class method in objective c. Using self or classname? objective-c objective-c

Accessing class method in objective c. Using self or classname?


If you are calling another class method from inside a class method (of the same class) you can just use [self classMethod]. If however you are in an instance method and you need to call that classes class method you can use [[self class] classMethod]

As pointed out by @Martin R - if you subclass PlayingCard, calling self in a class method will then be that subclass and not PlayingCard.

EDIT:

For completeness you need to do:

//  PlayingCard.m#import "PlayingCard.h"@implementation PlayingCard@synthesize suit = _suit; + (NSArray *)validSuits {    return @[@"♠︎", @"♣︎", @"♥︎", @"♦︎"];}+ (NSArray *)rankStrings {    return @[@"?", @"A", @"2", @"3", @"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"Q",@"K"];}+ (NSUInteger)maxRank {    return [[self rankStrings] count] -1; }//override super class's method- (NSString *)contents {    NSArray *rankStrings = [[self class] rankStrings];      return [rankStrings[self.rank] stringByAppendingString:self.suit];}- (void) setSuit:(NSString *)suit {    if ( [[[self class] validSuits] containsObject:suit]) {         _suit = suit;    }}- (NSString *) suit {    return _suit ? _suit : @"?";}@end