user2717159 user2717159 - 5 months ago 11
Objective-C Question

calling overridden subclass's class method from abstract superclass

Supposing I have an abstract superclass (e.g., 'Shape') and it has two subclasses (e.g., 'Circle' and 'Triangle'). Shape has a class method which is overridden in Circle and Triangle. If I want any given Shape to call this method on itself, but I don't know which subclass will be calling the method until runtime, how can I call it? E.g.,

@implementation Shape
+(void)iAmA{
NSLog(@"I'm an abstract method");
}

-(void)identifySelf {
[???? iAmA]; //call relevant subclass's class method
}
@end


and

@implementation Triangle
+(void)iAmA{
NSLog(@"I'm a triangle");
}
@end

@implementation Circle
+(void)iAmA{
NSLog(@"I'm a circle");
}
@end


So, an instance of a Shape subclass should be able to call identifySelf, and make use of it's own class's iAmA method. In Swift, I would do this with the Self keyword. How can I do this in Objective-C? (Thanks, and please let me know if I need to clarify the problem)

Answer
- (void)identifySelf {
    [[self class] iAmA];
}