[英]Objective-C: Cannot access synthesized variable
I'm using Cocos2d and looking to access a variable (CakesEaten) from another CCScene. 我正在使用Cocos2d,希望从另一个CCScene访问变量(CakesEaten)。
// in MainScene.h
@interface MainScene : CCScene{
int CakesEaten;
}
@property int CakesEaten;
// in MainScene.m
@implementation
@synthesize CakesEaten;
// at the top of ScoreScreen.m
#import "MainScene.h"
// in the ScoreScreen.m init method
MainScene.CakesEaten = 9999
Gives an error of: Property "CakesEaten" not found on object of type MainScene. 给出以下错误:在MainScene类型的对象上找不到属性“ CakesEaten”。 It's the only error that I receive when building.
这是我在构建时收到的唯一错误。
In order to access any property, synthesized or not, you need an instance of the class: 为了访问任何属性(无论是否综合),您都需要一个该类的实例:
MainScene *scene = [[MainScene alloc] init];
scene.CakesEaten = 9999;
Note: the latest editions of Xcode do not require @synthesize
, unless you would like to change the name for the variable that is used by default. 注意:最新版本的Xcode不需要
@synthesize
,除非您想更改默认使用的变量的名称。 You can safely remove that line, along with the declaration of the member variable. 您可以安全地删除该行以及成员变量的声明。
The end result should look like this: 最终结果应如下所示:
@interface MainScene : CCScene
@property (nonatomic, readwrite) int CakesEaten;
@end
不是MainScene,而是自我。
self.CakesEaten = 9999
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.