0

我正在使用《Learning Cocos2D》一书中的模式。我有一个 GameState 类,它是一个可以保存为状态的单例。第一次创建类 gest 时,BOOL soundOn 将 init 设置为 false,但数组 levelProgress 不存在。我对尝试初始化数组的所有行发表了评论。该类使用加载和保存数据的助手。当我尝试 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

解决方案: *我刚刚添加了 av init 方法... *

-(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 回答