ios - how to access a sub class object property if just give the super class object -
i have super class chartmodel, , sub classes chartmodel, in there model called linechartmodel, , has property categorytype.
now have method accept 1 parameter (datamodel *) like:
- (void) showchartwithmodel:(chartmodel *)datamodel { if ([datamodel respondstoselector:@selector(categorytype)]) { if ([datamodel categorytype] == categorytypedate) { // compiler error } } } now compiler complains [datamodel categorytype], because categorytype not defined in chartmodel. do not want put categorytype super class, because not every chart has such property. how fix this? thanks.
you cast datamodel id:
- (void) showchartwithmodel:(chartmodel *)datamodel { if ([datamodel respondstoselector:@selector(categorytype)]) { if ([(id)datamodel categorytype] == categorytypedate) { // compiler error } } } or cast linechartmodel, in case that's i'd test for:
- (void) showchartwithmodel:(chartmodel *)datamodel { if ([datamodel iskindofclass:[linechartmodel class]]) { if ([(linechartmodel *)datamodel categorytype] == categorytypedate) { // compiler error } } }
Comments
Post a Comment