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
Using
propertynamejust 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.propertynamegenerally uses an accessor. If you’re using@synthesize, the generated accessors will handle memory management as specified in your@propertyline (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
@propertyand@synthesizeto handle your variables, then use theself.propertynameaccessors to reduce the memory management burden on yourself. The guide also recommends you avoid implementing custom accessors (i.e. using@propertywithout@synthesize).