通过“propertyname”访问属性之间的区别与“self.propertyname”相比在 Objective-C 中?

发布于 2024-10-21 00:18:08 字数 597 浏览 11 评论 0原文

在 Objective-C 中通过“propertyname”与“self.propertyname”访问属性之间有什么区别?您能否在回答中介绍一下:

  1. 什么是最佳实践?
  2. 这两种方法如何影响内存管理(保留计数/内存管理的责任)
  3. 任何其他优点/缺点

该场景的假设可以基于以下内容:

头文件

@interface AppointmentListController : UITableViewController {
    UIFont *uiFont;
}
@property (nonatomic, retain) UIFont *uiFont;

实现

- (void)viewDidLoad {
    [super viewDidLoad];  

    uiFont = [UIFont systemFontOfSize:14.0];
    //VERSUS
    self.uiFont = [UIFont systemFontOfSize:14.0];

谢谢

What is the nce between accessing a property via "propertyname" versus "self.propertyname" in objective-c? Can you cover in the answer:

  1. What is best practice?
  2. How do the two approaches affect memory management (retain counts / one's responsibilities for memory management)
  3. Any other advantages/disadvantages

The assumption for the scenario could be based on the following:

Header file

@interface AppointmentListController : UITableViewController {
    UIFont *uiFont;
}
@property (nonatomic, retain) UIFont *uiFont;

Implementation

- (void)viewDidLoad {
    [super viewDidLoad];  

    uiFont = [UIFont systemFontOfSize:14.0];
    //VERSUS
    self.uiFont = [UIFont systemFontOfSize:14.0];

thanks

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

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

发布评论

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

评论(2

哑剧 2024-10-28 00:18:08

使用 propertyname 仅访问实例变量。您负责对其内容进行自己的内存管理;不会为您执行任何保留或释放操作。

使用self.propertyname通常使用访问器。如果您使用 @synthesize,生成的访问器将按照您的 @property 行中指定的方式处理内存管理(您给出的示例使用 retain ,因此在为 self.propertyname 设置新值时将执行保留)。您还可以编写自己的访问器方法来根据需要进行管理。

更完整的解释位于 内存管理编程指南。这种情况下的最佳实践通常是使用 @property@synthesize 来处理变量,然后使用 self.propertyname 访问器来减少内存管理给你自己带来负担。该指南还建议您避免实现自定义访问器(即使用@property而不使用@synthesize)。

Using propertyname just accesses the instance variable. You're responsible for doing your own memory management on its contents; no retains or releases are performed for you.

Using self.propertyname generally uses an accessor. If you're using @synthesize, the generated accessors will handle memory management as specified in your @property line (the example you gave uses retain, so a retain will be performed on setting a new value to self.propertyname). You can also write your own accessor methods that do management as you like.

A fuller explanation is in the Memory Management Programming Guide. Best practices in this case are generally to use @property and @synthesize to handle your variables, then use the self.propertyname accessors to reduce the memory management burden on yourself. The guide also recommends you avoid implementing custom accessors (i.e. using @property without @synthesize).

停顿的约定 2024-10-28 00:18:08

附加说明 - 它对于 iPhone 来说不太有用,因为 Cocoa Touch 中没有绑定。但如果您使用 Cocoa,请注意以下几点很有用:

键值编码。 KVC 是整个 Cocoa 中使用的协议,尤其是在绑定中。它将首先查找您的密钥的访问器,并且仅作为最后的手段直接访问数据。您可以通过实现访问器来缩短 KVC 的搜索,从而加快数据访问速度。

另请注意,如果直接设置实例变量,以 var = value 的形式,Key-Value Observing 将不会注意到更改,并且绑定对象将不会获取新值。

An additional note - It's not so useful for the iPhone, since there aren't bindings in Cocoa Touch. But if you're using Cocoa, it's useful to note the following:

Key-Value Coding. KVC is a protocol used throughout Cocoa, most notably in bindings. It will look for accessors for your keys first, and only access data directly as a last resort. You can shorten KVC's search and thus speedup data access by implementing accessors.

Also be aware that if you set instance variables directly, in the form of var = value, Key-Value Observing will not notice the change and bound objects will not get the new value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文