访问另一个类xcode中的变量

时间:2011-12-05 16:33:25

标签: iphone objective-c 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);

当我运行HelloWorld Layer时,输出为0.我将场景更改为class2文件并将myInt更改为100并且输出为100.但是当我将场景替换为HelloWorldLayer时,输出再次为0而不是100.请帮忙,谢谢你。

1 个答案:

答案 0 :(得分:2)

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

然后,当您引用self.myIntmyLayer.myInt时,您会引用HelloWorldLayerStatic.myInt。这有点像黑客,但它确实有效。