Reputation: 165
ViewController.h
@property (strong,nonatomic) NSMutableArray *products;
@property (strong, nonatomic) IBOutlet UITableView *tableViewProducts;
i would like to know apple or developer recommended method of accessing instance variables and accessor methods.
Should i use _instanceVariable or self.instanceVariable or should i synthesize all ivars ?
Method 1
ViewController.m
@synthesize products;
@synthesize tableViewProducts;
@synthesize productCount;
......
UITableViewCell *cell=[tableViewProducts dequeueReusableCellWithIdentifier:@"cellReuseIdentifier"];
productCount.
Method 2
ViewController.m
UITableViewCell *cell=[_tableViewProducts dequeueReusableCellWithIdentifier:@"cellReuseIdentifier"];
Method 3
ViewController.m
UITableViewCell *cell=[self.tableViewProducts dequeueReusableCellWithIdentifier:@"cellReuseIdentifier"];
Upvotes: 0
Views: 78
Reputation: 16170
You doesn't need to @synthesize
all properties. But Can @synthesize
property to create custom Setter Method.
self.propertyName
will trigger this setter Method & _propertyName
won't trigger.
Example:
.h
@property(string, nonatomic) NSString *name;
.m
@synthesize name = _name;
- (void)setName:(NSString *)name {
_name = name;
}
Upvotes: 0
Reputation: 13333
Always access properties with the accessor methods, so always use self.property
etc. Except, in init
(and initWithWhatever
etc) methods, in which you should always access the backing variable directly _property
etc. This is to avoid side effects of accessing self
before self
has finished initializing.
The reason you always want to use self.property
is because that enables useful side effects. The accessor methods can be overridden to validate values, trigger KVO effects (automatically update views for example), use default values if no specific value has been set and much more. Those are bypassed if you use the backing variables directly.
Upvotes: 2
Reputation: 691
Nowadays you don't need to @synthesize
properties. Since you've already defined your properties you even don't need to user the instance variable, simple use the property itself as self.instanceVariable
.
Upvotes: 0
Reputation: 318824
You haven't needed to use @synthesize
for years now so don't bother adding those lines.
Since you have properties, access the properties, not the underlying instance variables.
This means your 3rd option is the best choice.
Upvotes: 2