需要有关Objective-c属性概念的帮助 [英] Need assistance regarding Objective-c properties concept

查看:76
本文介绍了需要有关Objective-c属性概念的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Apple Doc 用于了解属性实例变量,但有点困惑

I am reading Apple Doc for understanding property instance variable but bit confused

从Apple Doc:

From Apple Doc:

大多数属性由实例变量支持默认情况下, readwrite属性将由实例变量支持,该实例变量将 再次由编译器自动合成.

Most Properties Are Backed by Instance Variables By default, a readwrite property will be backed by an instance variable, which will again be synthesized automatically by the compiler.

实例变量是存在的变量,并保留其值为 对象的寿命.用于实例变量的内存是 在首次创建对象时(通过alloc)分配并释放对象时分配 当对象被释放时.

An instance variable is a variable that exists and holds its value for the life of the object. The memory used for instance variables is allocated when the object is first created (through alloc), and freed when the object is deallocated.

除非另行指定,否则合成实例变量具有 与属性同名,但带有下划线前缀.为一个 名为firstName的属性,例如,合成实例 变量将称为_firstName.

Unless you specify otherwise, the synthesized instance variable has the same name as the property, but with an underscore prefix. For a property called firstName, for example, the synthesized instance variable will be called _firstName.

尽管最佳做法是对象访问其自身的属性 使用访问器方法或点语法,可以访问 直接从类中的任何实例方法中获取实例变量 执行.下划线前缀可以清楚地表明您 访问实例变量而不是例如本地变量 变量:

Although it’s best practice for an object to access its own properties using accessor methods or dot syntax, it’s possible to access the instance variable directly from any of the instance methods in a class implementation. The underscore prefix makes it clear that you’re accessing an instance variable rather than, for example, a local variable:

如果最好使用访问器方法或点语法,那么为什么要使用_ivarPropertyName?

If using accessor methods or dot syntax is best practice then why user _ivarPropertyName?

为什么要使用ivar来呈现属性?有什么好处?当苹果说使用访问器方法或点语法是最佳做法"时

Why use ivar for presenting properties? what are its benefits? when apple says "using accessor methods or dot syntax is best practice"

推荐答案

@property声明属性的存在(描述其接口),但未指定该属性的实现.但是属性需要将其内容存储在某处.默认情况下,编译器为此合成一个 ivar (以及匹配的setter和getter).因此,通常您可以忽略 ivar 的存在,而只需使用 dot语法.

@property declares the existence of a property (describing its interface), but doesn't specify the implementation of that property. But properties need to store their contents somewhere. By default, the compiler synthesizes an ivar for that (and matching setters and getters). So normally you can ignore the existence of the ivar and just use dot syntax.

我遵循Apple的建议,并尝试避免直接使用 ivars .但是有时您想访问属性而不调用其getter.我的代码中最常见的异常是延迟初始化的只读属性:

I follow Apple's advice and try to avoid using ivars directly. But somtimes you want to access a property without invoking its getter. The most common exception in my code is lazily-initialized read-only properties:

@interface MyObject : NSObject
@property ( nonatomic, readonly ) id someProperty ;
@end

@implementation MyObject
@synthesize someProperty = _someProperty ; // required; compiler will not auto-synthesize ivars for readonly properties

-(id)someProperty
{
    if ( !_someProperty )
    {
        _someProperty = ... create property here
    }

    return _someProperty ;
}

@end

此外,您可能不想调用-dealloc方法中的属性的getter方法,例如,计时器属性.为避免在-dealloc中创建计时器,请直接访问ivar:

Also, you may not want to invoke the getter for a property in your -dealloc method... for example, a timer property. To avoid creating a timer in -dealloc, access the ivar directly:

-(void)dealloc
{
    [ _myTimer invalidate ] ; // don't use self.myTimer here, that would create a timer even though we're going away...
}

可能会有更多的用例.对于大多数属性,您甚至不需要使用ivar,只需使用<value> = self.propertyself.property = <new value>.

There are probably more use cases. For most properties you don't even need to use the ivar, just use <value> = self.property and self.property = <new value>.

此外,与直接访问 ivar 相比,通过消息分发(使用 dot-accessor语法或getter)访问属性会产生一些额外开销.几乎在所有情况下都不会有什么改变.

Also, there will be some additional overhead for accessing the property via message dispatch (using dot-accessor syntax or the getter) vs directly accessing the ivar, but it will make no difference in almost all cases.

这篇关于需要有关Objective-c属性概念的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆