Auto property synthesis (@property) and inheritance Auto property synthesis (@property) and inheritance ios ios

Auto property synthesis (@property) and inheritance


I'd suggest to explicitly synthesize the property on your MutableCar implementation. As in:

@implementation MutableCar@synthesize name;@end

That way clang won't try to use autosynthesis

Edit:

If you don't want to use encapsulation and for other reason you need to access the ivar from the parent class, then you need to do a little bit more effort:

First the Car .h file remains the same (I added a printVar method to print the ivar and property):

@interface Car : NSObject- (void)printVar;@property (strong, readonly) NSString *name;@end

Now on the .m file, I'm implementing the printVar method and also adding a class extension to tell clang to create the setter:

// Private class extension, causes setName: to be created but not exposed.@interface Car ()@property (strong, readwrite) NSString *name;@end@implementation Car- (void)printVar{    NSLog(@"<Car> Hello %@, ivar: %@", self.name, _name);}@end

Now you can create your MutableCar.h as before:

@interface MutableCar : Car@property (strong, readwrite) NSString *name;@end

and your MutableCar.m should looks like this:

@implementation MutableCar@dynamic name;- (void)printVar{    [super printVar];    NSLog(@"<MutableCar> Hello %@", self.name);}@end

That way the _name ivar on the parent is actually written using the parent setter and you can access it.