Methods on concrete subclass are not called properly
I'm trying create a superclass whom I know will be subclassed (I would make the object abstract, but according to Creating an abstract class in Objective-C , you can't). The superclass defines a method, -initWithArguments:
, that just throws an error because it's overridden by subclasses. The superclass also has a static initializer that determines which subclass to return at runtime, like so:
Class subClass = NSClassFromString(classNameFromFile);
NSArray *argumentArray = [ArgumentsFromFile componentsSeparatedByString:@","];
SuperClass *returnObject = [[subClass alloc] initWithArguments:argumen开发者_运维技巧tArray];
if([returnObject isKindOfClass:[SuperClass class]]){
return returnObject;
}
I expect that the overridden -initWithArguments:
method will be called here in line 3. However, when I run this code, only the superclass's -initWithArguments:
ever gets called. I have checked and double checked that the overridden method signature is correct, and I've confirmed that subClass correctly represents the subclass. What the hell? Does this have to do with the fact that I'm calling the -initWithArguments:
inside the superclass? Thanks in advance for help; I love you all.
Either your subclass isn't really overriding -initWithArguments:
, or you aren't properly loading your subclass. So:
- Put a breakpoint in your subclass's
-initWithArguments:
. - Be sure there's no typo in the method name on the subclass: if you have, for instance,
-initwithArumgnetns:
in the subclass, the superclass's-initWithArguments:
will still be called. - Are you overriding
-allocWithZone:
or something somehow? If so, I'll bet a million dollars that your bug is there, if that's the case.
Best of luck!
精彩评论