Expose a private Objective-C method or property to subclasses

One way to solve this is to re-declare the property in your subclass’s class extension, and then add an @dynamic statement so that the compiler won’t create an overriding implementation of that property. So something like:

@interface SuperClass ()

@property (nonatomic, strong) id someProperty;

@end

....


@interface SubClass ()

@property (nonatomic, strong) id someProperty;

@end

@implementation SubClass

@dynamic someProperty;

@end

This obviously isn’t ideal because it duplicates a privately visible declaration. But it is quite convenient and helpful in some situations so I’d say evaluate on a case-by-case basis the dangers involved in this duplication vs. exposing the property in the public interface.

An alternative – that is used by Apple in UIGestureRecognizer – is to declare the property in a separate category header file explicitly named as “private” or “protected” e.g. “SomeClass+Protected.h”. That way, other programmers will know they ought not import the file. But, if you don’t control the code you’re inheriting from, that’s not an option.

Leave a Comment