Search code examples
objective-ciosinheritancemethodsenforcement

iOS Subclassing and compulsory methods


I have a base view controller that a number of other view controllers subclass. Is there a way of enforcing certain methods which have to be overridden in a subclass?

For safety sake more than anything.

Cheers


Solution

  • In other languages this is done using abstract classes and methods. In Objective-C there is no such thing.

    The closest you can get is raising an exception in the superclass so subclasses are 'forced' to override them.

    [NSException raise:NSInternalInconsistencyException format:@"Subclasses must override %@", NSStringFromSelector(_cmd)];