Suppose I have a property called myPropertyName
defined in my class MyClassName
. Manual memory management is used throughout this post.
MyClassName.h
#import <UIKit/UIKit.h>
@interface MyClassName : NSObject {
@private
NSObject* myPropertyName;
@public
}
@property (nonatomic, retain) NSObject* myPropertyName;
// Some methods prototypes are here
@end
MyClassName.m
#import "MyClassName.h"
@implementation MyClassName
@synthesize myPropertyName;
// Some methods are here
@end
I'm confused with usages such as the place of myPropertyName
declaration, its difference between instance variable. For example, what is the difference among these three statement of initialization code, for example, in the customized -(void)init
method for my class myClassName
.
self.myPropertyName = [[[NSObject alloc] init] autorelease];
This one is calling
myPropertyName
setter, but I'm not sure what is the name of the instance variable being used in the setter,myPropertyName
(since I've declared a @private field namedmyPropertyName
) or_myPropertyName
(people say that this one with underbar is the default)?myPropertyName = [[NSObject alloc] init];
Does this initialize the instance variable of the
myPropertyName
property? If I don't have@synthesize myPropertyName = _myPropertyName;
, would it be wrong since the default instance variable for the property is said to be_myPropertyName
._myPropertyName = [[NSObject alloc] init];
Is
_myPropertyName
still declared as the instance variable for my propertymyPropertyName
even if I use@synthesize myPropertyName;
and@private NSObject* myPropertyName;
?
In my understanding, a property is just a name (such as myPropertyName
), there should be some instance variable encapsulated to be used in actual operations in the code, such as assigning values.