我無意中發現了這個代碼從蘋果:爲什麼Apple創建ivars如果@synthesize創建它們?
@interface TiltShift : CIFilter
{
CIImage *inputImage;
NSNumber *inputRadius;
}
@property (retain, nonatomic) CIImage *inputImage;
@property (copy, nonatomic) NSNumber *inputRadius;
@end
@implementation TiltShift
@synthesize inputImage;
@synthesize inputRadius;
蘋果創造的屬性和在同一時間創建實例變量和合成它們。我的問題是:蘋果爲什麼要這樣做?
據我知道的Xcode甚至會沒有@synthesize
創建的聲明的屬性下劃線的ivars。所以,我們已經創建了_inputImage
和_inputRadius
,另一個奇怪的事情是,在給出這個代碼的情況下,Xcode如何知道Apple希望將這些屬性與ivars沒有下劃線相關聯。
我會做簡單:
@interface TiltShift : CIFilter
{
// CIImage *inputImage;
// NSNumber *inputRadius;
}
@property (retain, nonatomic) CIImage *inputImage;
@property (copy, nonatomic) NSNumber *inputRadius;
@end
@implementation TiltShift
// @synthesize inputImage;
// @synthesize inputRadius;
,並有下劃線的ivars。
而這不僅僅是蘋果。我在網上看到了很多來自其他開發人員的代碼,這樣做,我已經看到了最近的代碼。在我看來,這是不好的代碼,因爲它不可能辨別「真正的」ivars和備份屬性的ivars。
創建這樣的代碼是否有意義?
非常舊的示例代碼。這幾乎是幾年前所需要的。 Objective-C幾年前發展起來,只需要'@ property'就行了。 – rmaddy