ObjC ivar/global var declaration – a clarification

You can declares iVar-s in header files but it’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 firstiVar;

@synthesize anyProperty;

The compiler will treat it like other variables outside the class. So they will be global variables. 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 1, and later you set it to 2 in another instance, the value will be 2 for both (since you only have one instance in fact instead of two). AND since it wasn’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.

WARNING: you won’t get any warning or error. It could be a pain in your ass to find these kind of bugs.

So just to clarify:

int firstVar; // global variable, shared and accessible in the whole project and all instances
static int firstStaticVar; // global variable shared among all instances, accessible from only this file

@implementation MyClass {
    int firstiVar;
static firstStaticIVar;// compile error, cannot do this
}

int secondiVar; // !!! the same as firstVar !!!
static secondStaticIVar; // the same as firstStaticVar

@synthesize anyProperty;

Good luck!