user1050548
user1050548

Reputation: 381

Access variables in another class xcode

//In class2.m File,

 HelloWorldLayer myHelloWorldLayer = [[HelloWorldLayer alloc]init];
 myHelloWorldLayer.myInt =100;
        NSLog(@"%i",myHelloWorldLayer.myInt);

//In HelloWorldLayer.h
int _myInt;
@property (nonatomic,readwrite) int myInt;


//In HelloWorldLayer.m
@synthesize myInt= _myInt;

NSLog(@"%i",self.myInt);

When i run HelloWorld Layer, output is 0. I changed scene to class2 file and changed myInt to 100 and the output is 100. But when i replace scene back to HelloWorldLayer output is 0 again instead of 100. Please help, thank you.

Upvotes: 0

Views: 1509

Answers (1)

Richard J. Ross III
Richard J. Ross III

Reputation: 55583

// in HelloWorldLayer.h
// these are static variables. 
// You would reference them like so: 
// HelloWorldLayerStatic.myInt = x;
static struct { 
   int myInt; 
} HelloWorldLayerStatic;

Then, when you would refer to self.myInt or myLayer.myInt, you would refer to HelloWorldLayerStatic.myInt. It's a little bit of a hack, but it works.

Upvotes: 2

Related Questions