Heisenberg
Heisenberg

Reputation: 367

Does "@synthesize" every "@property" necessary?

I'm following one of the iOS tutorials from Ray Wenderlich (Scarybugs part 1). But I notice for each property in the model, he always "@synthesize" it in the implementation.

Here is the example of the models:

#import <Foundation/Foundation.h>

@interface RWTScaryBugData : NSObject

@property (strong) NSString *title;
@property (assign) float rating;

- (id)initWithTitle:(NSString*)title rating:(float)rating;

@end

--

#import "RWTScaryBugData.h"

@implementation RWTScaryBugData

@synthesize title = _title;
@synthesize rating = _rating;

- (id)initWithTitle:(NSString*)title rating:(float)rating {
  if ((self = [super init])) {
    self.title = title;
    self.rating = rating;
  }
  return self;
}

@end

--

#import <Foundation/Foundation.h>

@class RWTScaryBugData;

@interface RWTScaryBugDoc : NSObject

@property (strong) RWTScaryBugData *data;
@property (strong) UIImage *thumbImage;
@property (strong) UIImage *fullImage;

- (id)initWithTitle:(NSString*)title rating:(float)rating thumbImage:(UIImage *)thumbImage fullImage:(UIImage *)fullImage;

@end

--

#import "RWTScaryBugDoc.h"
#import "RWTScaryBugData.h"

@implementation RWTScaryBugDoc
@synthesize data = _data;
@synthesize thumbImage = _thumbImage;
@synthesize fullImage = _fullImage;

- (id)initWithTitle:(NSString*)title rating:(float)rating thumbImage:(UIImage *)thumbImage fullImage:(UIImage *)fullImage {  
  if ((self = [super init])) {
    self.data = [[RWTScaryBugData alloc] initWithTitle:title rating:rating];
    self.thumbImage = thumbImage;
    self.fullImage = fullImage;
  }
  return self;
}

@end

I know "@synthesize" is basically to allocate an instance variable for a property, but it has been taken care of by default for every "@property" in ".h file" (although not visible).

My questions is: is it necessary to "@synthesize" every "@property" we have in our public API? (I tried deleting all the "@synthesize" in the implementation, and it still worked)

Upvotes: 1

Views: 305

Answers (3)

Anurag Bhakuni
Anurag Bhakuni

Reputation: 2439

hope this will help little more.

@property(nonatomic) NSString *name;

the @property is an Objective-C directive which declares the property

-> The "`nonatomic`" in the parenthesis specifies that the property is non-atomic in nature.
-> and then we define the type and name  of our property.
-> prototyping of getter and setter method

now go to .m file

previously we have synthesis this property by using @synthesis , now it also NOT required , it automatically done by IDE.

-> this @synthesis now generate the getter and setter(if not readonly) methods.

and Then why we even write @synthesis in our code if it always done by IDE .

one of the basic use is :-

what our IDE do internally

@synthesis name=_name;

we use _name to access particular property but now you want synthesis by some other way like firstname you can do it like

@synthesis name= firstname

or just by name

@synthesis name=name

So from it you can access this property as you want.

Upvotes: 0

Anurag
Anurag

Reputation: 141869

@synthesize is no longer needed. The compiler will synthesize the getter and setter as required with an instance variable named as _<propertyName> automatically. It creates the instance variable but more importantly it creates the getter and setter methods (for readwrite properties).

If you've manually provided the getter/setter for a property, then an instance variable won't be automatically synthesized, and you'll need to add the @synthesize statement. From the docs:

Note: The compiler will automatically synthesize an instance variable in all situations where it’s also synthesizing at least one accessor method. If you implement both a getter and a setter for a readwrite property, or a getter for a readonly property, the compiler will assume that you are taking control over the property implementation and won’t synthesize an instance variable automatically. If you still need an instance variable, you’ll need to request that one be synthesized: @synthesize property = _property;

Upvotes: 4

Ken Thomases
Ken Thomases

Reputation: 90531

As noted in the Objective-C Feature Availability Index, automatic synthesis of property instance variables was introduced with Xcode 4.4 (LLVM Compiler 4.0) and requires the modern runtime (all code on iOS, 64-bit code on OS X).

So, the tutorial is a bit dated, that's all.

Upvotes: 1

Related Questions