I have an Objective-c class “MyClass”. In MyClass.m I have a class extension that declares a CGFloat property:
@interface MyClass ()
@property (nonatomic) CGFloat myFloat;
@end
@implementation MyClass
@synthesize myFloat;
//...
@end
What changes (if anything) when the property is declared using the readonly keyword?
@interface MyClass ()
@property (nonatomic, readonly) CGFloat myFloat;
@end
@implementation MyClass
@synthesize myFloat;
//...
@end
Perhaps in the first case I can say self.myFloat = 123.0; and CGFloat f = self.myFloat; inside MyClass? Then in the second case the readonly keyword prevents the assignment self.myFloat = 123.0; but allows the read CGFloat f = self.myFloat;
The option
readonlymeans that only the getter method is being declared for this property. Thus, without a setter, it can’t be modified viamyObject.myFloat=0.5f;If you don’t declare it
readonly, it’sread writeby default.Declaring your property via () extension does not modify the access mode but it modifies the scope; it will be a “private” property.