Alex Bollbach
Alex Bollbach

Reputation: 4570

Synthesized iVar not accessible from subclass

I have the following code which gives the compiler error "Instance Variable '_someBool' is prive".

@interface Foo: NSObject
@property (assign) BOOL someBool;
@end
@implementation Foo
@end

@interface Bar: Foo
@end
@implementation Bar
- (BOOL)someBool {
    // subclass specific getter logic
    return _someBool;
}
@end

Why can't I access the storage of the property that was inherited? And what would be the proper way to override this getter?

Upvotes: 0

Views: 179

Answers (1)

CRD
CRD

Reputation: 53000

A getter is just a method, if you wish to override a method and call its original implementation you use super:

- (BOOL)someBool
{
   // subclass specific getter logic
   return [super someBool];
}

Why can't I access the storage of the property that was inherited?

It is private. If you really want to access the storage from a subclass then specify your own storage by declaring an @protected instance variable and using @synthesise property=variable. Using super is probably better.

Upvotes: 1

Related Questions