Jon Rossen <
jon...@comcast.net> 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 Parker
gpa...@apple.com Runtime Wrangler