With XCode 5.1, a new warning appears. It made me understand -obviously- that I was doing something wrong.
The idea was to have an object (a model) and it's mutable version which inherits from the original class. So the idea is to open a property which was readonly
to readwrite
@interface Car : NSObject
@property (strong, readonly) NSString *name;
@end
@interface MutableCar : Car
@property (strong, readwrite) NSString *name;
@end
Those needs to be in separate files (like two normal classes).
And it gives this warning :
Auto property synthesis will not synthesize property 'name' because it is 'readwrite' but it will be synthesized 'readonly' via another property
So I would like to know what is the right solution to do something like it, if it's even possible. if it's needed to write accessors and avoid using auto synthesis, etc. Just please be precise and support your answer with documentation or whatever.
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.