Prior to ARC, if I wanted a property to be readonly to using it but have it writeable within the class, I could do:
// Public declaration
@interface SomeClass : NSObject
@property (nonatomic, retain, readonly) NSString *myProperty;
@end
// private interface declaration
@interface SomeClass()
- (void)setMyProperty:(NSString *)newValue;
@end
@implementation SomeClass
- (void)setMyProperty:(NSString *)newValue
{
if (myProperty != newValue) {
[myProperty release];
myProperty = [newValue retain];
}
}
- (void)doSomethingPrivate
{
[self setMyProperty:@"some value that only this class can set"];
}
@end
With ARC, if I wanted to override setMyProperty, you can't use retain/release keywords anymore so is this adequate and correct?
// interface declaration:
@property (nonatomic, strong, readonly) NSString *myProperty;
// Setter override
- (void)setMyProperty:(NSString *)newValue
{
if (myProperty != newValue) {
myProperty = newValue;
}
}
You can redeclare your property as
readwrite
in interface extension:Yes, that is adequate, but you don't even need that much.
You can do
The compiler will do the right thing here.
The other thing though, is you don't even need THAT. In your class extension you can actually respecify
@property
declarations.Doing that, you just need to synthesize and you have a private setter that is synthesized for you.