1

Sorry about the slightly vague question title but I found it very difficult to get it straight in my head myself.

The issue here is that I have two different dataSources I might be initialising and loading data from. Depending on the data changes which dataSource I need.

The problem I am having is how to define a variable of that dataSource when it could come from two different classes.

If I define them in my interface:

BColumnChartDataSource * chartDatasource = [[BColumnChartDataSource alloc] initWithExercise:_exercise];
BDoubleColumnChartDataSource * chartDatasource = [[BDoubleColumnChartDataSource alloc] initWithExercise:_exercise];

Then it obviously doesn't like them being called the same thing.

If I try to put them in an if statement then they aren't available outside the logic statement

if (_exercise.unitTypeLinks.count < 2) {
    BColumnChartDataSource * chartDatasource = [[BColumnChartDataSource alloc] initWithExercise:_exercise];
    }
else {
    BDoubleColumnChartDataSource * chartDatasource = [[BDoubleColumnChartDataSource alloc] initWithExercise:_exercise];
}

Eventually I want to put them into a statement like this so I could put an if statement into every one of these but it is an awfully verbose way which might take more time if I add more dataSources.

// Get the exercise event list for our clicked exercise
_exerciseEventList = [chartDatasource getExerciseEventList];

I think I must be missing something obvious here so thank you for any help you can give

sam_smith
  • 6,023
  • 3
  • 43
  • 60
  • `id chartDatasource` seems like the obvious solution here. It kind of depends on how similarly the two classes operate, though. – Farski Feb 27 '14 at 04:36
  • The chartDatasources are pretty similar - just little tweaks to the chart - how would I use id ChartDatasource in the code? Just put it at the top? – sam_smith Feb 27 '14 at 04:38
  • BColumnChartDataSource and BDoubleColumnChartDataSource are 2 classes, depending on the satiation you need to get information from that class(where you will get this situation in many controllers) is this your question – Charan Giri Feb 27 '14 at 05:03

3 Answers3

1

The easiest way is to make sure that BColumnChartDataSource and BDoubleColumnChartDataSource have a common super class. For example, write a super class called BDataSource and make sure that both of the other class are subclass of this.

If that is too difficult to do, the easiest thing to do (which I don't recommend) is to make sure the property is id or NSObject, then do the type checking every time you access the property. This is definitely not ideal and you shouldn't do this. The right thing to do is the previous paragraph.

Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
1
NSObject *chartDataSource;
if (_exercise.unitTypeLinks.count < 2) {
    chartDataSource = [[BColumnChartDataSource alloc] initWithExercise:_exercise];
}
else {
    chartDataSource = [[BDoubleColumnChartDataSource alloc] initWithExercise:_exercise];
}
//Now do something with chartDataSource

that handles the exact example you describe, although it leaves quite a bit to be desired as downstream consumers of chartDataSource will probably have to do their own conditioning upon the result of ([chartDataSource isKindOfClass:[BColumnChartDataSource class]])

Better patterns are likely to be found somewhere in the idea of "inheritance", depending on what the actual differences of your two dataSource classes actually are.

ryan cumley
  • 1,901
  • 14
  • 11
1

You should create create a base class, and inherit both of those classes with the base class.

@interface BDataSource : NSObject
@end

@interface BColumnChartDataSource : BDataSource
//your custom implementation here
@end

@interface BDoubleColumnChartDataSource : BDataSource
//your custom implementation here
@end

After that you can initialise your datasource like this

BDataSource *dataSource = nil;

if (_exercise.unitTypeLinks.count < 2) {
    dataSource = [[BColumnChartDataSource alloc]     initWithExercise:_exercise];
}
else {
    dataSource = [[BDoubleColumnChartDataSource alloc] initWithExercise:_exercise];
}
Afnan
  • 888
  • 1
  • 13
  • 37