0

私は本LearningCocos2Dのパターンを使用しています。状態として保存できるシングルトンのGameStateクラスがあります。BOOL soundOnは、クラスgestが最初に作成されたときにfalseとして初期化されますが、配列levelProgressはdosentです。配列を初期化しようとしたすべての行にコメントを付けました。このクラスは、データをロードおよび保存するヘルパーを使用します。1または2を試してみると、「クラスメソッドでアクセスされるインスタンス変数'levelProgress'」エラーが発生します。

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface GameState : NSObject <NSCoding> {
    BOOL soundOn;
    NSMutableArray *levelProgress; 
}

+ (GameState *) sharedInstance;
- (void)save;

@property (assign) BOOL soundOn;
@property (nonatomic, retain) NSMutableArray *levelProgress;
@end


#import "GameState.h"
#import "GCDatabase.h"

@implementation GameState

@synthesize soundOn;
@synthesize levelProgress;

static GameState *sharedInstance = nil;

+(GameState*)sharedInstance {
    @synchronized([GameState class]) 
    {
        if(!sharedInstance) {
            sharedInstance = [loadData(@"GameState") retain];
            if (!sharedInstance) {
                [[self alloc] init]; 
                // 1. levelProgress = [[NSMutableArray alloc] init];
            }
        }
        return sharedInstance; 
    }
    return nil; 
}

+(id)alloc 
{
    @synchronized ([GameState class])
    {        
        NSAssert(sharedInstance == nil, @"Attempted to allocate a \
                 second instance of the GameState singleton"); 
        sharedInstance = [super alloc];
        // 2. levelProgress = [[NSMutableArray alloc] init];
        return sharedInstance; 
    }
    return nil;  
}

- (void)save {
    saveData(self, @"GameState");
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    // 3. levelProgress = [[NSMutableArray alloc] init];
    [encoder encodeBool:currentChoosenCountry forKey:@"soundOn"];
    [encoder encodeObject:levelProgress forKey:@"levelProgress"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    if ((self = [super init])) {
        // 4. levelProgress = [[NSMutableArray alloc] init];    
        soundOn = [decoder decodeBoolForKey:@"soundOn"];
        levelProgress = [decoder decodeObjectForKey:@"levelProgress"];
    }
    return self;
}
@end

解決策: * avinitメソッドを追加しました... *

-(id)init {
    self = [super init];
    if (self != nil) {
        levelProgress = [[NSMutableArray alloc] init];
    }
    return self;
}
4

3 に答える 3

1

これを試してください(このコードには構文エラーまたは論理エラーが含まれている可能性があります-メモ帳で記述しました):

@interface GameState : NSObject <NSCoding>

@property (nonatomic, readwrite) BOOL soundOn;
@property (nonatomic, retain) NSMutableArray *levelProgress;

+ (GameState *)sharedState;
- (void)writeDataToCache;

@end

//

@implementation GameState

@synthesize soundOn, levelProgress;

#pragma mark - Singleton

static GameState *sharedState = nil;

+ (void)initialize {
    static BOOL initialized = NO;
    if (!initialized) {
        initialized = YES;
        if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/gameState", pathCache]]) {
            NSData *encodedObject = [[NSData alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/gameState", pathCache]];
            data = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
        }
        else
            data = [[GameState alloc] init];
    }
}
+ (GameState *)sharedState {
    return sharedState;
}

#pragma mark - Initialization

- (id)init {
    if (self = [super init]) { // will be inited while application first run
        soundOn = NO;
        levelProgress = [[NSMutableArray alloc] init];

        return self;
    }
    return nil;
}

#pragma mark - Coding Implementation

- (void)writeDataToCache { // use this method to save current state to cache
    NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:self];
    if([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/GameState", pathCache]])
        [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/GameState", pathCache] error:nil];
    [[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/GameState", pathCache] contents:encodedObject attributes:nil];
    NSLog(@"GameState was saved successfully.");
}
- (void)encodeWithCoder:(NSCoder*)encoder {
    [encoder encodeBool:self.soundOn forKey:@"soundOn"];
    [encoder encodeObject:self.levelProgress forKey:@"levelProgress"];
}
- (id)initWithCoder:(NSCoder*)decoder {
    if ((self = [super init])) {
        self.soundOn = [decoder decodeBoolForKey:@"soundOn"];
        self.levelProgress = [decoder decodeObjectForKey:@"levelProgress"];

        NSLog(@"GameState was inited successfully");
        return self;
    }
    return nil;
}

@end
于 2012-04-04T08:16:54.957 に答える
0

1と2は間違いです。インスタンスを指定せずにインスタンス変数にアクセスすることはできません。

行をに変更することで1を修正できますsharedInstance.levelProgress = ...

2は悪い考えです。メソッドを使用して初期化するという一般的な慣習に違反してinit...いるため、後で同じコードで作業する他のプログラマーにとっては驚きで問題が発生する可能性があります。

3と4はloadData問題ありませんが、失敗した場合は実行されず、オブジェクトは通常で初期化されinit、配列はnilポインターになります。

[[self alloc] init];

initまた、そこでプロパティをオーバーライドして初期化する必要があります。

于 2012-04-04T08:17:39.710 に答える
0

1と2は同じであり(levelProgressはインスタンス変数であるため、機能しないはずです)、3と4は配列を新たに初期化するのではなく、エンコード/デコードする必要があります。

于 2012-04-04T08:01:47.213 に答える