Home > Software design >  What is the difference between synthesize propertyname = _propertyname
What is the difference between synthesize propertyname = _propertyname

Time:10-13

I am new to objective c. I create a property at .h header file of the class and then i synthesize.

What is the difference between

@synthesize propertyname;

and

@synthesize propertyname = _propertyname;

Both work but what is the purpose of using the first one or the second one.

Any help appreciated.

CodePudding user response:

First of all you need not write @synthesize propertyname anymore.

In Objective-C when you declare a property, compiler auto generates its accessor methods for you by default. These accessor methods can be getter and setter if property is both read and write property else just a getter if it's a readonly property.

Compiler uses internal variables (called iVar) under the hood of these accessor methods implementation. (You can obviously provide your own implementation of these accessor methods and also provide your own internal variables as well)

What is the difference between @synthesize propertyname; and @synthesize propertyname = _propertyname;

when you declare a @synthesize propertyname; compiler auto generates and uses an iVar named propertyname and uses it in its default accessor implementation and you can also use it as propertyname in your .m file as compiler has already declared it for you.

property = @"abcd";

when you declare a @synthesize propertyname = _propertyname; compiler auto generates and uses a iVar named _propertyname and uses it in its default accessor implementation and you can also use it as _propertyname in your .m file as compiler has already declared it for you.

_property = @"abcd";

Broadly speaking the general difference between the two statement is just the change in name of iVar (usage of it are different though)

what is the purpose of using the first one or the second one.

As I already mentioned you need not use either of them in a normal usecase. Compiler already does that for you.

We used to write @synthesize propertyname; when we neither had any specific reservation about the name of ivar nor wanted to provide our own private variable as ivar to a specific property (Explained in detail below). This was simpler than explicitly specifying the name of iVar and we knew that compiler will generate an ivar with same name as property for us.

we typically wrote @synthesize propertyname = _propetyname when @synthesize propertyname; wasn't available or when we wanted our iVar to follow a specific nomenclature ('_' following name of property) or when we wanted to use our own private variable as iVar to a property.

In both the cases @synthesize was handy because it would relieve us from writing a boiler plate code like adding setter and getter methods for properties declared.

How to use custom variable as ivar for a property?

@interface SynthesizeExplorer : NSObject
@property (nonatomic,strong) NSString *name;
@end

@implementation SynthesizeExplorer
NSString *blaBlaName;
@synthesize name = blaBlaName;
@end

If you look at the implementation you will see that property name is backed by a internal variable named blaBlaName and not by the typical compiler generated ivar like name or _name.

As of today the only reason I see for the usage of @synthesize when you wanna use your custom internal variable to back the property you have declared. Otherwise I dont see any point in writing neither @synthesize propertyname; or @synthesize propertyname = _propertyname;

  • Related