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 ?

有帮助吗?

解决方案

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?

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top