synthesize vs dynamic in Objective-C

1 minute read

At the core of Object Orientation you should not access data directly, instead you should use accessor methods to get or set the data. In Objective-C @property directive is a simplified way in which developers can tell the compiler about the behavior required for the accessor methods, which is simple and brief.

Now the question arise, where are the accessor methods? This leads us to the key words which are our topic today i.e @synthesize and @dynamic.

@synthesize

@synthesize propertyName = _propertyName;

@synthesize tells the compiler to take care of the accessor methods creation i.e it will generate the methods based on property description. It will also generate an instance variable to be used which you can specify as above, as a convention it starts with _(underscore)+propertyName.

@dynamic

@dynamic propertyName;

mentioning @dynamic with a propertyName tells the compiler not to create accessor methods because user will provide the implementation dynamically in future. Apple allows its developers to provide method implementation at runtime using Dynamic Method Resolution.

Further Reading