Objective-C Properties – Make Property Readonly for Outside Class Calls

cocoaobjective-c

Would you know a way to make a property readonly for outside calls and readwrite for inside calls ?

I've read times ago somthing that seemed like

In the .h

@property(nonatomic, readonly) NSDate* theDate;

In the .m

@interface TheClassName()
@property(nonatomic, retain) NSDate* theDate;
@end

but this raises a warning when compiling the .m "Property theDate attribute in TheClassName class continuation does not match class TheClassName property".

Anyway, it seems to work (can read but not set from outside the class, can do both from inside) but I should have missed somehting to avoid the warning.
Or if you know a better way to do this…

Best Answer

In your .h:

@property(nonatomic, retain, readonly) NSDate* theDate;

In your .m:

@interface TheClassName()
@property(nonatomic, retain, readwrite) NSDate* theDate;
@end
Related Question