I have come to find that many of the times in which I want to have a synthesized readonly property, I merely implement the getter method of that property in terms of other variables with no need for an ivar, for example (Note: I am defining ivars in the interface because I am using OmniGraffle UML software and it does not recognize ivars auto-generated by synthesized properties):
@interface Editor : UIView {
BOOL _wordWrap;
BOOL _showLineNumbers;
NSDictionary *_options;
}
@property (nonatomic) BOOL wordWrap;
@property (nonatomic) BOOL showLineNumbers;
@property (nonatomic, copy, readonly) NSDictionary *options;
@end
@implementation Editor
@synthesize wordWrap = _wordWrap;
@synthesize showLineNumbers = _showLineNumbers;
@synthesize options = _options;
- (NSDictionary *)options {
return @{
@"WordWrap" : [NSNumber numberWithBool:self.wordWrap],
@"ShowLineNumbers" : [NSNumber numberWithBool:self.showLineNumbers],
};
}
@end
In the above Editor
class, is it necessary for me to define the _options
ivar in the header definition and more importantly does the auto-generated ivar take up memory or space in the symbol table? Also, would it be more efficient to use copy
, retain
, or no value in this case? Just curious.