简体   繁体   中英

Objective-C: Cannot access synthesized variable

I'm using Cocos2d and looking to access a variable (CakesEaten) from another CCScene.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM