私のシングルトンクラスのプロパティへのアクセスに問題があります。 私はこのコードを書いている:Singleton.h - 私はwordNumberを設定し、別のシーンcocos2dのシングルトンクラス。変数へのアクセス
[Singleton sharedSingleron].wordNumber = wordNum;
[[CCDirector sharedDirector] replaceScene: [CCTransitionFlipAngular transitionWithDuration:0.3 scene:[WordsLayer node]]];
に行く私の最初のシーンで
#import "Singleton.h"
@implementation Singleton
@synthesize wordNumber = _wordNumber;
static Singleton* _sharedSingleton = nil;
+(Singleton *) sharedSingleron{
@synchronized([Singleton class])
{
if (!_sharedSingleton)
[[self alloc] init];
return _sharedSingleton;
}
return nil;
}
+(id)alloc
{
@synchronized([Singleton class])
{
NSAssert(_sharedSingleton == nil, @"Attempted to allocate a second instance of a singleton.");
_sharedSingleton = [super alloc];
return _sharedSingleton;
}
return nil;
}
-(id)init {
self = [super init];
if (self != nil) {
}
return self;
}
@end
-
#import <Foundation/Foundation.h>
@interface Singleton : NSObject{
int _wordNumber;
}
+ (Singleton *) sharedSingleron;
@property (readwrite) int wordNumber;
@end
Singleton.mファイルをWordsLayer.mでこのプロパティを取得しようとします。
-(id) init
{
if((self=[super init])) {
int wordNum = [Singleton sharedSingleron].wordNumber;
}
return self;
}
しかしwordNumの値が間違っています。間違いを見つけるのを助けてください。
は、現在の値と(シングルトンクラスの)プロパティの実装のNSLogに追加するようにしてください。私はそれがあなたを助けることができると思います。 – OdNairy
あなたは_sharedSingletonを決して設定しません.... –