{"id":17,"date":"2012-06-26T20:45:13","date_gmt":"2012-06-26T20:45:13","guid":{"rendered":"http:\/\/andras.palfi.hu.rose.arvixe.com\/?p=17"},"modified":"2012-06-26T20:45:13","modified_gmt":"2012-06-26T20:45:13","slug":"objc-properties-vs-ivars","status":"publish","type":"post","link":"https:\/\/andras.palfi.hu\/?p=17","title":{"rendered":"ObjC Properties vs iVars"},"content":{"rendered":"<p>\t\t\t\tAbout property and iVar usage in init\/dealloc methods.<\/p>\n<p>Technically one use properties if he wants to publish a member. Make it accessible and\/or modifiable without exposing the structure of the class. Properties also allows you to run a code (accessors) when the property is set (setter method) or get (getter method) by someone.<\/p>\n<p>Since Xcode @synthesize generates the getter\/setter for a property automatically, private properties are started to use as a convenient way to store a reference by retaining\/copying an object.\u00a0Moreover it isn&#8217;t needed to explicitly define an ivar for a property. The iVar is automatically generated now.<\/p>\n<p>The properties however can do some more than one expects. Of course it automatically deallocates the original object assigning a new value to it but also sends out KVO notifications and there can be additional code which run hitting the accessor.<\/p>\n<h3>Usage in init\/dealloc is not recommended<\/h3>\n<p>There are more reasons why you shouldn&#8217;t have to call a property in Init\/dealloc:<\/p>\n<ul>\n<li>Subclasses have not yet been initialized or have already deallocated when init or dealloc methods execute, making accessor methods potentially unreliable<\/li>\n<li>The setter can be an implemented method with other tasks or can be overridden in a descendant class and you never know what else will run calling the setter.<\/li>\n<li>Moreover KVO notifications fire when the property changes and there can be listeners with additional code which also will run at that moment.<\/li>\n<\/ul>\n<p>Either of them are desirable to run in a dealloc.<\/p>\n<p>&nbsp;\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>About property and iVar usage in init\/dealloc methods. Technically one use properties if he wants to publish a member. Make it accessible and\/or modifiable without exposing the structure of the class. Properties also allows you to run a code (accessors) when the property is set (setter method) or get (getter method) by someone. Since Xcode [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"hide_page_title":"","footnotes":""},"categories":[3,4],"tags":[],"_links":{"self":[{"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/posts\/17"}],"collection":[{"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17"}],"version-history":[{"count":0,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions"}],"wp:attachment":[{"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}