以我从未见过的方式保留财产?

发布于 2024-10-24 04:34:40 字数 285 浏览 1 评论 0原文

我看到了一个带有以下代码的示例代码:

在 .h 文件中:

  CALayer *_animationLayer;
  @property (nonatomic, retain) CALayer *animationLayer;

在 .m 文件中:

  @synthesize animationLayer = _animationLayer;

我猜它与保留计数有关?

有人可以解释一下吗?

I saw a sample code with this code:

in the .h file:

  CALayer *_animationLayer;
  @property (nonatomic, retain) CALayer *animationLayer;

And in the .m file:

  @synthesize animationLayer = _animationLayer;

I am guessing it is related to the retain count?

Can someone explain that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偷得浮生 2024-10-31 04:34:40

将其视为变量名的别名。

来自 Cocoa 基础指南

@synthesize 的语法也是
包括一个扩展,允许您
使用不同的名称
属性及其实例变量
贮存。例如,考虑
以下声明:

@synthesize title, directReports, role = jobDescrip;

这告诉计算机合成访问器方法
属性 titledirectReports
角色并使用jobDesrip
支持角色的实例变量
财产。

Consider it like an alias for the variable name.

From the Cocoa Fundamentals Guide:

The syntax for @synthesize also
includes an extension that allows you
to use different names for the
property and its instance-variable
storage. Consider, for example, the
following statement:

@synthesize title, directReports, role = jobDescrip;

This tells the computer to synthesize accessor methods for
properties title, directReports, and
role, and to use the jobDescrip
instance variable to back the role
property.

つ低調成傷 2024-10-31 04:34:40

.h 文件中的代码声明了两件事,一个名为 _animationLayer 的变量,其类型为 CALayer*,以及一个名为 animationLayer 的属性,也是CALayer*类型。 .m 文件中的代码指示 Objective-C 编译器自动为 animationLayer 属性生成 getter 和 setter,使用 _animationLayer 变量来保存实际值放。

例如:

_animationLayer = nil;  //initialize the variable to nil
self.animationLayer = [[CALayer alloc] init];  //assign a value to the property
NSLog("_animationLayer is:  %@", _animationLayer);  //it's not set to nil anymore
_animationLayer = nil;  //set it to nil again
NSLog("self.animationLayer is:  %@", self.animationLayer);  //now the property is nil

你是对的,这确实与对象的retainCount有一些关系(因此将变量视为属性的直接别名并不完全正确)。本质上,直接使用_animationLayer变量设置值不会保留新值或释放旧值。使用属性访问器设置值即可。例如:

_animationLayer = [[CALayer alloc] init];  //retain count is 1
self.animationLayer = nil;                 //retain count is 0

self.animationLayer = [[CALayer alloc] init];  //oops, retain count is 2
self.animationLayer = nil;  //retain count is 1, the object leaked

_animationLayer = [[CALayer alloc] init];  //retain count is 1
[_animationLayer release];               //retain count is 0
self.animationLayer = nil;               //oops, just released it again

The code in the .h file is declaring two things, a variable called _animationLayer which is of type CALayer*, and a property called animationLayer which is also of type CALayer*. The code in the .m file is instructing the objective-c compiler to automatically generate getters and setters for the animationLayer property, using the _animationLayer variable to hold the actual value that is set.

So for instance:

_animationLayer = nil;  //initialize the variable to nil
self.animationLayer = [[CALayer alloc] init];  //assign a value to the property
NSLog("_animationLayer is:  %@", _animationLayer);  //it's not set to nil anymore
_animationLayer = nil;  //set it to nil again
NSLog("self.animationLayer is:  %@", self.animationLayer);  //now the property is nil

And you're correct, this does have some relationship to the object's retainCount (so it's not quite correct to think of the variable as a direct alias for the property). In essence, setting the value directly using the _animationLayer variable does not retain the new value or release the old value. Setting the value using the property accessor does. For example:

_animationLayer = [[CALayer alloc] init];  //retain count is 1
self.animationLayer = nil;                 //retain count is 0

self.animationLayer = [[CALayer alloc] init];  //oops, retain count is 2
self.animationLayer = nil;  //retain count is 1, the object leaked

_animationLayer = [[CALayer alloc] init];  //retain count is 1
[_animationLayer release];               //retain count is 0
self.animationLayer = nil;               //oops, just released it again
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文