I have stumbled on this code from Apple:
@interface TiltShift : CIFilter
{
CIImage *inputImage;
NSNumber *inputRadius;
}
@property (retain, nonatomic) CIImage *inputImage;
@property (copy, nonatomic) NSNumber *inputRadius;
@end
@implementation TiltShift
@synthesize inputImage;
@synthesize inputRadius;
Apple is creating properties and at the same time creating ivars and synthesizing them. My question is: why is Apple doing this?
As far as I know Xcode will create underscore ivars for the declared properties even without @synthesize
. So, we will have _inputImage
and _inputRadius
already created and another strange thing is, given this code, how will Xcode know that Apple wants to associate the properties with the ivars without underscore.
I would do simply this:
@interface TiltShift : CIFilter
{
// CIImage *inputImage;
// NSNumber *inputRadius;
}
@property (retain, nonatomic) CIImage *inputImage;
@property (copy, nonatomic) NSNumber *inputRadius;
@end
@implementation TiltShift
// @synthesize inputImage;
// @synthesize inputRadius;
and have the underscore ivars.
And this is not just Apple. I have seen a lot of code on the web, from other developers, doing this and I have seen that on recent code. In my opinion this is bad code because it will make impossible to discern between "real" ivars and ivars backing up properties.
Does it make sense to create code like this?
TLDR - this is old code, before ARC, before property autosynthesis. We are talking pre-2011 (or 2010?) code here.
@synthesize
won't create underscore ivars. It will create ivars with the same name as is the name of the property.
That's why in the old code we were using things like @synthesize inputImage = _inputImage;
to have the ivar properly underscored.
The modern autosynthesis (when you don't specify @synthesize
at all) will create underscored ivars.
You know, historically underscored variables were reserved for Apple so nobody was supposed to use them.