Use 'MutableClass' methods in 'Class' methods

41 views Asked by At

Hi all,

Consider the following :

// Things.h
@interface Things : NSObject
@property (strong, nonatomic) NSSomething * listOfThings;
@end

// Things.m
@implementation Things
// Some methods
@end

// MutableThings.h
@interface MutableThings : Things
- (instancetype) thingsPassingTest;
@end

// MutableThings.m
@implementation MutableThings
- (instancetype)thingsPassingTest
{
    // Do some stuff in order to return a subset of Things
    return newThings;
}
@end

I would like to be able to call thingsPassingTest for a Things object, and not a MutableThings.

Is there a way to call a subclass method ?

Or maybe am I going in the wrong direction ?

2

There are 2 answers

2
Greg On BEST ANSWER

In your design your MutableThings inherits from Thinks so every public property and methods implemented in Things will be available in MutableThings object. You can override thingsPassingTest in MutableThings class and return nil, maybe this is what you want. But the best way is to create root class with all of the common methods for Things and MutableThings classes. And implement thingsPassingTest just in Things not in the MutableThings and not in the root class. Is it something you asking for?

0
Hashim Khan On

You can not call "thingsPassingTest" on "Things" object because "Things" is superclass for "MutableThings". It is against the rules of object Oriented Programing paradigm.