ios – NSCoding和保存/加载默认值问题

我正在为iPhone应用程序编写一些代码,而且我遇到了正确加载默认数据的问题.我的代码基于Ray Wenderlich的“Learning Cocos2d”一书中的一些例子.

似乎即使我直接删除应用程序并尝试从新数据开始,应用程序不一致或者不尝试加载数据,或者错误地认为存在数据,并且加载null.

我正在使用containsValueForKey来检查值是否存在,然后加载它或加载一些默认值,但即使在全新安装中,containsValueForKey也会查找数据并且不会加载默认值.在xcode的组织者中,我检查了我的设备的文件结构和我指定保存的Documents文件夹,看起来它不包含任何文件,所以我不确定它是什么.

我的猜测是问题与initWithCoder函数有关.它有时似乎神秘地经历了这个功能,但并非总是如此.另一个奇怪的事情是,当玩家获得高分时(此处未显示,但代码与此ObjectiveList完全相同,只有一个int),我调用[[GameManager sharedGameManager] save]并且它似乎正确保存.

现在代码:

GCDatabase.h

#import <Foundation/Foundation.h>
id loadData(NSString * filename);
void saveData(id theData, NSString *filename);

GCDatabase.m

#import "GCDatabase.h"
NSString * pathForFile(NSString *filename) {
// 1
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
// 2
NSString *documentsDirectory = [paths objectAtIndex:0];
// 3
return [documentsDirectory stringByAppendingPathComponent:filename];
}

id loadData(NSString * filename) {

NSString *filePath = pathForFile(filename);
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

    NSData *data = [[[NSData alloc] initWithContentsOfFile:filePath] autorelease];
    NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:data] autorelease];
    id retval = [unarchiver decodeObjectForKey:@"Data"];
    [unarchiver finishDecoding];
    return retval;
}
return nil;
}

void saveData(id theData, NSString *filename) {

NSMutableData *data = [[[NSMutableData alloc] init] autorelease];
NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease];
[archiver encodeObject:theData forKey:@"Data"];
[archiver finishEncoding];
[data writeToFile:pathForFile(filename) atomically:YES];
}

GameManager.h

@interface GameManager : NSObject <NSCoding>{
NSMutableArray *objectiveDescriptions;
}
@property (nonatomic, retain) NSMutableArray * objectiveDescriptions;
+(GameManager*)sharedGameManager;
-(void)save;
-(void)load;
-(void)encodeWithCoder:(NSCoder *)encoder;
-(id)initWithCoder:(NSCoder *)decoder;

@end

GameManager.m(我添加了加载功能,试图强制它加载,但它似乎不起作用)

+(GameManager*)sharedGameManager {
@synchronized([GameManager class])
{
    if(!sharedGameManager) {
        sharedGameManager = [loadData(@"GameManager") retain];
        if (!sharedGameManager) {
            [[self alloc] init];
        }
    }
    return sharedGameManager;
}
return nil; 
}

+(id)alloc {
@synchronized([GameManager class]){
    NSAssert(sharedGameManager == nil, @"Attempted to allocate a second instance of the Game Manager singleton");
    sharedGameManager = [super alloc];
    return sharedGameManager;
}
return nil;
}

- (void)dealloc {
[objectiveList release];
[super dealloc];
}

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

-(void)load {
loadData(@"GameManager");
}

- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:objectiveList forKey:@"objectiveList"];
}

- (id)initWithCoder:(NSCoder *)decoder  {
self = [super init];
if (self != nil) {
if ([decoder containsValueForKey:@"objectiveList"]) {
        objectiveList = [decoder decodeObjectForKey:@"objectiveList"];
    } else {
        [objectiveList addObjectsFromArray:[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil]];
    }
}

return self;
}
@end

最佳答案 我还没有读完你的完整代码..但我在代码中发现了一个问题….

你还没有为objectiveList数组分配内存.除非你将内存分配给数组,否则不会添加对象……

我想去吧

 objectiveList = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil]];

代替

    [objectiveList addObjectsFromArray:[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil]];

检查语法..希望这可能会有所帮助,因为它也困扰我,在过去我忘了为数组分配内存..并继续添加导致null的对象… 🙂
如果它没有解决你的问题,我会在以后完全寻找代码.. 🙂

点赞