{"id":220,"date":"2012-07-24T23:48:00","date_gmt":"2012-07-24T21:48:00","guid":{"rendered":"http:\/\/andras.palfi.hu\/?p=220"},"modified":"2012-07-24T23:48:00","modified_gmt":"2012-07-24T21:48:00","slug":"objc-ivar","status":"publish","type":"post","link":"https:\/\/andras.palfi.hu\/?p=220","title":{"rendered":"ObjC ivar\/global var declaration &#8211; a clarification"},"content":{"rendered":"<p>\t\t\t\tYou can declares iVar-s in header files but it&#8217;s better in the implementation, so they will be really invisible for others.<br \/>\nYou should do this way:<\/p>\n<pre class=\"lang:objc decode:true\">@implementation MyClass {\n    int firstiVar;\n}\n@synthesize anyProperty;<\/pre>\n<p>But what if you forget the parenthesis and you put the variable simply beside the @synthesize?<br \/>\nLike this:<\/p>\n<pre class=\"lang:objc decode:true\">@implementation MyClass\n\nint firstiVar;\n\n@synthesize anyProperty;<\/pre>\n<p>The compiler will treat it like other variables\u00a0<strong>outside<\/strong> the class. So they will be <strong>global variables<\/strong>. Unfortunately in this case these variables is not just shared among all instances but shared in the whole project. What does it mean? It means if you set the value in one instance to\u00a0<strong>1<\/strong>, and later you set it to\u00a0<strong>2<\/strong> in another instance, the value will be 2 for both\u00a0(since you only have one instance in fact instead of two). AND since it wasn&#8217;t marked as static if you have an other global variable with the same name that will be shared together with the variables in the class, even if you did it accidentally.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>WARNING: you won&#8217;t get any warning or error. It could be a pain in your ass to find these kind of bugs.<\/strong><\/span><\/p>\n<p>So just to clarify:<\/p>\n<pre class=\"lang:objc decode:true\">int firstVar; \/\/ global variable, shared and accessible in the whole project and all instances\nstatic int firstStaticVar; \/\/ global variable shared among all instances, accessible from only this file\n\n@implementation MyClass {\n    int firstiVar;\nstatic firstStaticIVar;\/\/ compile error, cannot do this\n}\n\nint secondiVar; \/\/ !!! the same as firstVar !!!\nstatic secondStaticIVar; \/\/ the same as firstStaticVar\n\n@synthesize anyProperty;<\/pre>\n<p>Good luck!<\/p>\n<p>&nbsp;\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can declares iVar-s in header files but it&#8217;s better in the implementation, so they will be really invisible for others. You should do this way: @implementation MyClass { int firstiVar; } @synthesize anyProperty; But what if you forget the parenthesis and you put the variable simply beside the @synthesize? Like this: @implementation MyClass int [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","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\/220"}],"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=220"}],"version-history":[{"count":0,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/posts\/220\/revisions"}],"wp:attachment":[{"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}