1

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:argumentArray];
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.

Community
  • 1
  • 1
AnthonyJoeseph
  • 223
  • 3
  • 10

1 Answers1

3

Either your subclass isn't really overriding -initWithArguments:, or you aren't properly loading your subclass. So:

  1. Put a breakpoint in your subclass's -initWithArguments:.
  2. 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.
  3. 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!

Jonathan Sterling
  • 18,320
  • 12
  • 67
  • 79
  • I'm overriding -allocWithZone: way over on the other side of the program, in an unrelated singleton. Would that do it? I've checked and double checked that my method signatures are correct, and I've debugged and made certain that I'm properly loading my subclass. I already put a breakpoint in both my implementations, and the compiler always chooses the wrong one. – AnthonyJoeseph Feb 13 '11 at 22:49
  • 1
    I fixed it! I feel pretty stupid. It was a problem with my file data. Thanks anyway, though. I hope I gave you some mental exercise that will put you ahead of the game. – AnthonyJoeseph Feb 13 '11 at 23:05