0
//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.

4
  • Could you be a bit more clear? Do you need to change the value of one instance of a class, or do you want that value to be the same for all instances of that class? Commented Dec 5, 2011 at 16:36
  • I want to change myInt in HelloWorldLayer to 100. Commented Dec 5, 2011 at 16:39
  • I understand that part. What I do not understand is if you want that to be for all layers you make of that type, or just one of those layers you make. Commented Dec 5, 2011 at 16:41
  • I want that to be for all layers i make of that type. Like all layers should read myInt as 100 now. Commented Dec 5, 2011 at 16:46

1 Answer 1

2
// 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.

Sign up to request clarification or add additional context in comments.

3 Comments

May i know what is wrong with my code too? Thanks. I searched many answers and this method is supposed to work.
There was nothing wrong with your code, but when you use replaceScene with a new scene every time, you create a new instance of the scene, which thus replaces the instance variables, clearing myInt back to 0.
Also, please be sure to up vote and accept the answer if you can.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.