Use accessors everywhere, as much as possible.
Use them in
-initWithFrame:. If you provide an implementation for your own setter function, you'll need to synthesize it so you can access the private variable directly. The
@synthesize statement also helps as a documentation cue to indicate you are accessing the private variable somewhere.
Define a property like:
@property (nonatomic,strong) NSString username;
self.username everywhere, except in the
-(void)setUsername: function. And if you implement
-setUsername: you'll also need to do a:
This makes it easy to control access to data, implementing caching and lazy initialization, as well as trigger related updates or process. I implement setters often in
UIViewController subclasses to kick start a network fetch request for the data to be displayed in a view controller. For example,
-setUsername: with a non empty string can begin fetching a user's profile.