简体   繁体   English

是否建议为只读综合属性定义Ivars?

[英]Is it recommended to define ivars for readonly synthesized properties?

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): 我发现很多时候我想拥有一个合成的只读属性,例如,我只是根据其他变量实现了该属性的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

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? 在上面的Editor类中,是否有必要在标头定义中定义_options ivar, 重要的是,自动生成的ivar是否占用符号表中的内存或空间? Also, would it be more efficient to use copy , retain , or no value in this case? 另外,在这种情况下使用copyretain或不使用值会更有效吗? Just curious. 只是好奇。

First: stop putting your ivar declarations in your @interface . 首先:停止将ivar声明放在@interface They belong in your @implementation . 它们属于您的@implementation See this answer for a detailed explanation. 请参阅此答案以获取详细说明。

Anyway, given what you've written, your @synthesize options = _options has no effect. 无论如何,鉴于您所写的内容,您的@synthesize options = _options无效。

That @synthesize has two possible effects: @synthesize具有两个可能的效果:

  1. It adds an instance variable named _options , if your class doesn't have one. 如果您的类没有一个实例变量,它将添加一个名为_options的实例变量。

  2. It generates a getter method, options , that returns the value of _options , if your class doesn't have a method named options . 如果您的类没有名为options方法,它将生成一个getter方法options ,该方法返回_options的值。

Since you manually defined the instance variable and the getter, the @synthesize does nothing. 由于您手动定义了实例变量和getter,因此@synthesize不执行任何操作。 You can remove it entirely without changing the meaning of your program. 您可以完全删除它,而无需更改程序的含义。

Specifying copy on a readonly property has no effect. 在只读属性上指定copy无效。 The copy and retain (or, more properly under ARC, strong ) attributes only affect the generated setter method, and the compiler doesn't generate a setter for a readonly property. copyretain (或更确切地说,在ARC下, strong属性)仅影响生成的setter方法,并且编译器不会为readonly属性生成setter。 (If you change the property to readwrite in a class extension, then copy matters.) (如果将属性更改为在类扩展中为readwrite ,则进行copy 。)

Yes, the _options ivar takes up both memory (for each instance of Editor ) and space in the symbol table. 是的, _options ivar占用了符号表中的内存(对于每个Editor实例)和空间。 Since you're not using the _options ivar, you should delete it entirely. 由于您没有使用_options ivar,因此应将其完全删除。 You should also delete the @synthesize entirely, so the compiler doesn't generate the _options ivar for you. 您还应该完全删除@synthesize ,以便编译器不会为您生成_options ivar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM