Search code examples
iphoneobjective-ccocoa-touchcocoacocoa-design-patterns

Is there a good/best way to extend a nested class in Objective-C?


Sometimes framework objects put helper class interfaces inside of *.m files, such as:

Foo.m:

@interface HelperObject : NSObject
/*...*/
@end
@implementation HelperObject
/*...*/
@end

@implementation Foo
/*...*/
@end

If I want to extend Foo, for instance using a category, is there a way to extend HelperObject as well? More generally, is doing so a violation of encapsulation? Should I try to extend the class functionality without extending HelperObject?


Solution

  • Callers of Foo know nothing about HelperObject--frequently, they do not even know it exists. So no, it's not safe or valid to be subclassing it in another file.