Question

When subclassing an Apple class that lists private ivars in the .h file, is it ok to redeclare those ivars in your own subclass in the class extension @interface inside your subclass .m file in order to make them accessible to your subclass implementation?

Was it helpful?

Solution

You must bear in mind that declaring an instance variable in a subclass does not redeclare an existing superclass instance variable with the same name, regardless of the purported redeclaration being in the public interface or a class extension. Even if you use the same name, it is a different instance variable.

You can try this yourself. For example:

@interface Base : NSObject {
@private
    int _number;
}
@end

@implementation Base
- (id)init { self = [super init]; if (self) _number = 10; return self; }
- (void)logNumber { printf("base = %d\n", _number); }
@end

@interface Derived : Base
@end

@interface Derived () {
    int _number;
}
@end

@implementation Derived
- (id)init { self = [super init]; if (self) _number = 20; return self; }
- (void)logNumberDerived { printf("derived = %d\n", _number); }
@end

int main(void) {
    Derived *o = [Derived new];
    [o logNumber];
    [o logNumberDerived];
    return 0;
}

outputs:

base = 10
derived = 20

because _number in superclass is different from _number in subclass (extension). If you inspect the symbols in the binary output with nm -a, you’ll notice that the compiler generates two different symbols:

s _OBJC_IVAR_$_Base._number
s _OBJC_IVAR_$_Derived._number

OTHER TIPS

I have found here the answer that exactly matches my own: http://lists.apple.com/archives/cocoa-dev/2007/Feb/msg00939.html

If they made it @private and not @protected you would have to assume that it's for a reason. That reason could of course simply be that it's prudent to make everything @private until you know a good reason not to...

That said, it could very definitively not be OK to access it directly, even if it's technically possible.

The safe thing to do is to re-do everything in your own class, while petitioning the maintainer of the superclass to expose the functionality you need in the public / protected interface.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top