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...
In your .h:
@property(nonatomic, retain, readonly) NSDate* theDate;
In your .m:
@interface TheClassName() @property(nonatomic, retain, readwrite) NSDate* theDate; @end
This issue is largely eliminated if you're moving to ARC. Instead of two property declarations, you'd declare it once in the header.
@property(nonatomic, readonly) NSDate* theDate;
And then in the class extension, simply declare a __strong instance variable.
@interface TheClassName() { __strong NSDate* _theDate; } @end
And synthesize them appropriately in the implementation
@implementation TheClassName @synthesize theDate = _theDate;
Now you can set the instance variable.
_theDate = [NSDate date];
And ARC will magically inline the appropriate retain/release functionality into your code to treat it as a strong/retained variable. This has the advantage of being faster than the old style (retain) properties as well as ARC inlines the retain/release code at compile time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With