I have 2 classes in Cocoa, but am unable to access variables from one to the other.
Class1.h:
@interface MyClass : NSOpenGLView
{
int myVar;
}
@property (assign) int myVar;
Class1.m
@implementation MyClass
@synthetize myVar;
...
myVar=5;
Class2.m
MyClass *theClass=[MyClass alloc];
nb=theClass.myVar;
==> nb=0 (instead of 5), and I am sure that the myVar=5 was executed.
What did I wrong ?
Thanks !
In
Class2, you are creating a new instance ofMyClass, rather than referring to an existing instance where you have previously set themyVarproperty to 5.It’s also worth mentioning that, if you needed to create a new instance of
MyClass(that you don’t), this line:should be:
From Allocating and Initializing Objects: