On 7/19/2016 8:09 PM, Greg Parker wrote:
Jon Rossen <[email protected]> writes:
Let's say you have the following property:
@property foo;
Why am I seeing the following line of code to accompany the above
@property directive on various sites in discussions of @synthesize?:
@synthesize foo = _foo; // Obviously this ensures that the backing
// iVar for the foo property is named
// differently and follows the convention
// of the leading '_'.
Why is this @synthesize directive even needed? You don't need it anymore
to synthesize the setter and getter for the foo property. Secondly, if you >> omit it, by default, the backing iVar will be the same name as the
property prefixed with an '_'. In other words, by omiting this @synthesize >> line, you will automatically get what this @synthesize directive is
doing. It doesn't make sense to have it. Am I missing something?
There are three common reasons.
1. Automatic synthesis doesn't work on 32-bit Mac. You need the explicit @synthesize (or some other substitute) if your code needs to run there.
2. Automatic synthesis is relatively new. Historically you had to
use @synthesize (or some other substitute) on all platforms.
3. Some people distrust the possibility of hidden bugs caused by
automatic synthesis. They prefer to synthesize everything explicitly. Building with -Werror=objc-missing-property-synthesis enforces that.
Greg,
Thanks very much for this detailed answer. I find it interesting that automatic synthesis doesn't work on 32 bit Macs.
For #2 in your list (the newness of the feature), could that perhaps
suggest that some of the discussions I've been reading were from a few
years back? Sometimes I forget to check the dates on the site. And for discussions that were later on, after automatic synthesis was
implemented, could it also be that the people who post on the sites may
not be keeping up with latest features?
Another reason I was thinking of after my original post was code
readability. Apart from the distrust issue you cited in your #3, could
it be that some people like to explicitly synthesize because it makes
their code more self-documenting and easier for others to read?
thanks again,
jonR
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)