通过“propertyname”访问属性之间的区别与“self.propertyname”相比在 Objective-C 中?
在 Objective-C 中通过“propertyname”与“self.propertyname”访问属性之间有什么区别?您能否在回答中介绍一下:
- 什么是最佳实践?
- 这两种方法如何影响内存管理(保留计数/内存管理的责任)
- 任何其他优点/缺点
该场景的假设可以基于以下内容:
头文件
@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:
- What is best practice?
- How do the two approaches affect memory management (retain counts / one's responsibilities for memory management)
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
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 usesretain
, so a retain will be performed on setting a new value toself.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 theself.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
).附加说明 - 它对于 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.