在作为状态保存的单例中初始化NSMutableArray

时间:2012-04-04 07:57:39

标签: objective-c

我正在使用Learning Cocos2D一书中的模式。我有一个GameState类,它是一个可以保存为状态的单例。第一次创建类时,BOOL soundOn将init视为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

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

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

1&amp; 2是相同的(并且不应该工作,因为levelProgress是一个实例变量),3和4应该对数组进行编码/解码,而不是重新初始化它。

答案 2 :(得分:0)

1和2是错误的 - 如果不指定实例,则无法访问实例变量。

您可以通过将行更改为sharedInstance.levelProgress = ...来修复1。

2只是一个坏主意,你打破了使用init...方法初始化的常见惯例,它可能会让其他程序员感到惊讶并导致问题,这些程序员以后会处理相同的代码。

3和4都可以,但是如果loadData失败,它们将不会执行,对象将使用普通init进行初始化,数组将为nil指针:

[[self alloc] init];

你还应该覆盖init并初始化那里的属性。