this is my first post; this site has been an invaluable resource.
I’m fairly new to objective-c so please bear with.
So I have a base class with a few properties which I want “private” so I made them readonly. To be clear, I don’t want them mutable externally, but I DO wan’t to use the ‘set’ accessor within this class. So…
// .h file
@interface Vehicle
@property (nonatomic, readonly) int speed;
@end
Also I repeated the property declaration within a category interface block to make the accessors writable in this class
// .m file
//Private properties and methods
@interface Vehicle()
@property (nonatomic, readwrite) int speed;
@end
@implementation
@synthesize speed = _speed;
- (void) someMethod {
[self setSpeed:10]; // Works fine
}
@end
But now if I inherit this class the derived class no longer has the set accessor method (setSpeed in my case). Do I need to synthesize again? Seems like that would defeat the purpose of inheritence. I know i can modify the instance variable directly (_speed = 10;) but would rather not. I’m sure there’s something wrong with my understanding. Thanks!
// Example
@interface Ship : Vehicle
@end
@implementation
- (void) someOtherMethod {
[self setSpeed: 2]; // DOES NOT WORK, would like it to
}
@end
Actually, it does have the set accessor, it’s just that the compiler doesn’t know about it. You have a choice:
@interface Vehicle() .... @endbit in a separate header file that gets imported into the.mfor Vehicle and its subclasses (or use a category)@dynamic speedin the subclass’s implementation.