views:

70

answers:

1

Hi,

Why do two different instances of the same class are using their variables as if they where static variables?

I do not want to synthesize those variables.

But without synthesizing- both instances refer to their variables as if they where the same one.

Any way to go around it?

Why is that so?

Thanks-

Nir.

+1  A: 

By declaring the variables in the .m file (above the @implementation), you've declared global variables that are shared across instances.

Move the declarations to the .h file inside the @interface block.

@synthesize will only be needed if you also have the @property directive for the variables (unless you write getter/setter methods yourself).

DyingCactus
Thanks- and if the declaration is below the @implementation?
Tiger
As long as the declaration is outside a method and outside the @interface block, it is global. Global variables are not recommended.
DyingCactus
Thanks DyingCactus :)
Tiger
What about functions- if I declare a function only at the .m file?Will it also behave like a global?
Tiger
Putting methods outside @implementation will not compile. "Global methods" or methods not tied to an instance of a class are called "class methods" as opposed to "instance methods" and are created by putting a + sign in front of the method declaration instead of a minus sign. Read: http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html#//apple_ref/doc/uid/TP30001163-CH1-SW2 and: http://developer.apple.com/iphone/library/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/index.html#//apple_ref/doc/uid/TP40007594.
DyingCactus