【发布时间】:2014-08-31 00:56:45
【问题描述】:
我发现很多时候我想要一个综合的只读属性,我只是根据其他变量来实现该属性的 getter 方法,而不需要 ivar,例如 (注意:我在界面中定义 ivars,因为我使用的是 OmniGraffle UML 软件,它不能识别由合成属性自动生成的 ivars):
@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
在上面的Editor类中,我是否有必要在标题定义中定义_options ivar,更多重要的是,自动生成的ivar是否占用了内存或空间符号表?此外,在这种情况下使用copy、retain 或不使用值会更有效吗?只是好奇。
【问题讨论】:
-
你为什么要明确定义和合成 ivars?
-
你还没有使用 ARC 吗?
-
@vikingosegundo 我知道编译器会自动生成 ivars,但我使用的是 UML 软件(OmniGiraffe),它似乎无法处理由合成属性生成的 ivars。是的,我正在使用 ARC。我的代码中有什么东西让我看起来好像没有使用 ARC 吗??
-
"OmniGiraffe" --> "OmniGraffle" ?
-
@Martin R 是的,我就是这个意思
标签: ios objective-c properties synthesize ivars