I follow the idiom modeled by this example in many of my classes, so I can try to explain my justification for this practice.
The properties in this example are declared in the class extension in the .m file. This makes them virtually private. Any attempt to access these properties from another class will result in a "Property not found" error during compilation.
For developers coming from other languages, it may seem strange to synthesize getters and setters for private instance variables. Indeed, there is only one reason why I do this. When used continuously, synthesized properties can simplify memory management and help avoid careless errors that can lead to errors. Here are some examples:
Consider this:
self.workingPropertyString = [NSMutableString string];
in comparison with this:
workingPropertyString = [[NSMutableString string] retain];
Many developers claim that these two assignments are functionally equivalent, but there is an important difference. The second purpose of a memory leak is if workPropertyString has already pointed to a stored object. To write code functionally equivalent to a synthesized network device, you need to do something like this:
NSMutableString *newString = [NSMutableString string]; if (workingPropertyString != newString) { [workingPropertyString release]; workingPropertyString = [newString retain]; }
This code avoids the leakage of any existing object that can be referenced by an instance variable, and safely handles the possibility of re-assigning the same object to an instance variable. A synthesized setter does it all for you.
Of course, we can see that (workingPropertyString != newString) will always be true in this case, so we could simplify this task. In fact, in most cases, you probably avoid the simple direct assignment of an instance variable, but of course, these are exceptional cases that tend to create most errors. I prefer to play it safely and set all the instance variables of the object through synthesized setters. All assignments of objects of my instance are simple single-line ones that look like this:
self.foo = [Foo fooWithTitle:@"The Foo"];
or that:
self.foo = [[[Foo alloc] initWithTitle:@"The Foo"] autorelease];
This simplicity and consistency gives my weak brain less material to think about. As a result, I have almost no memory management errors. (I know that the autorelease idiom autorelease theoretically consume excessive memory in a narrow loop, but I have yet to face this problem in practice. If I ever do, this is a simple case for optimization.)
Another thing that I like about this practice is that my dealloc methods look like this:
- (void)dealloc { self.delegate = nil; self.dataToParse = nil; self.workingArray = nil; self.workingEntry = nil; self.workingPropertyString = nil; self.elementsToParse = nil; [super dealloc]; }
EDIT: Daniel Dickison pointed out some risk of using accessories in dealloc that I did not consider. See comments.
where each property of the object is simply set to nil. This simultaneously frees each saved property when it is set to zero, in order to avoid certain crashes due to EXC_BAD_ACCESS.
Note that I set self.delegate = nil; although this property has been declared as (nonatomic, assign) . This appointment was not strictly necessary. In fact, I could completely get rid of the properties for my objects (nonatomic, assign) , but again I found that the constant use of this idiom in all my instance variables makes my brain think less and further reduces the chance that I will create an error through some careless mistake. If necessary, I can simply flip the property from (nonatomic, assign) to (nonatomic, retain) without having to touch any memory management code. I like it.
You can also use consistency as an argument to synthesize properties for private scalar variables, as your example did in the case of BOOL storingCharacterData; . This practice ensures that every assignment to an instance variable looks like self.foo = bar; . Usually I’m not used to creating private scalar properties myself, but I see some excuses for this practice.