I have spent a few days learning Objective-C and have a few questions about @property. I have experience with C# so understand the need for pointers, initialization etc.
So as an example:
@interface MyClass : NSObject
{
IBOutlet UIImageView *image;
}
@property (retain, nonatomic) UIImageView *image
@end
@implementation MyClass
@synthesise image
@end
I understand that @synthesise is used to create the @property. But I have a few questions just to help me clear things up:
- Does the
@propertyduplicate or replace my original definition, or does it merely set up the mutibility and atomicity of the original? - Does
@synthesiseremove my need to useimage = [[UIImageView alloc] init]? - If I do not provide a
@propertyand still go ahead creating and destroying my variable manually, does that make any difference?
Ultimately, is the difference between the 2, @property gives you more flexibility with regards to memory management and multi-threading and the normal one gives you the defaults.
The ivar declaration of
imageis redundant when using the most recent compiler releases.The former declares an ivar (type + name + instance storage).
The property declaration specifies the type, name, storage (in more recent compiler releases), declares the accessor methods (e.g.
- (UIImageView *)image;and- (void)setImage:(UIImageView *)pImage;), and other property specifiers (which are used when the accessors are generated by the compiler).No. You still need to implement your initializer and
dealloc(in MRC) appropriately.That would be fine, when you do not want/need boilerplate accessor methods generated for you. It’s a design choice. Not every ivar needs accessor methods.
The biggest reason they exist is convenience. Properties save a lot of boilerplate code.
There is no more flexibility with properties — properties implement the most practical uses.
It’s infrequent that atomicity (in this context) is equivalent to proper thread safety and correct concurrent execution.