I'm using a framework which defines and uses 'ClassA', a subclass of NSObject. I would like to add some variables and functionality so naturally I created 'ClassB', a subclass of 'ClassA'
Now my problem is this. Many of the methods within this framework return instances of 'ClassA' which I would like to cast to my subclass.
For example take this method:
- (ClassA *)doSomethingCool:(int)howCool
Now in my code I try this:
ClassB * objB;
objB = (ClassB *)doSomethingCool(10);
NSLog(@"objB className = %@", [objB className]);
This runs just fine. No compile or runtime errors or anything. But what is really odd to me is the output:
>> "objB className = ClassA"
The casting obviously failed. Not sure what's happened at this point... objB is typed as 'ClassB', but it's className is 'ClassA' and it won't respond to any 'ClassB' methods.
Not sure how this is possible... Anyone know what I am doing wrong here?
I found a similar post which is exact opposite of what I'm asking here
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…