<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Accessor Methods on Nabeel's - iOS Developer Blog</title><link>https://nabeelarif.github.io/tags/accessor-methods/</link><description>Recent content in Accessor Methods on Nabeel's - iOS Developer Blog</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Tue, 25 Oct 2016 00:00:00 +0000</lastBuildDate><atom:link href="https://nabeelarif.github.io/tags/accessor-methods/index.xml" rel="self" type="application/rss+xml"/><item><title>synthesize vs dynamic in Objective-C</title><link>https://nabeelarif.github.io/p/synthesize-vs-dynamic-in-objective-c/</link><pubDate>Tue, 25 Oct 2016 00:00:00 +0000</pubDate><guid>https://nabeelarif.github.io/p/synthesize-vs-dynamic-in-objective-c/</guid><description>&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h1 id="synthesize"&gt;@synthesize
&lt;/h1&gt;&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-shell" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;@synthesize &lt;span class="nv"&gt;propertyName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; _propertyName&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;@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.&lt;/p&gt;
&lt;h1 id="dynamic"&gt;@dynamic
&lt;/h1&gt;&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-shell" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;@dynamic propertyName&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;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 &lt;a class="link" href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html" target="_blank" rel="noopener"
 &gt;Dynamic Method Resolution&lt;/a&gt;.&lt;/p&gt;
&lt;h1 id="further-reading"&gt;Further Reading
&lt;/h1&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html" target="_blank" rel="noopener"
 &gt;Dynamic Method Resolution&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="http://stackoverflow.com/questions/1160498/synthesize-vs-dynamic-what-are-the-differences" target="_blank" rel="noopener"
 &gt;@synthesize vs @dynamic, what are the differences?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item></channel></rss>