一連のNSDictionariesを使用して一部のデータを保存し、NSMutableArrayに保存し、NSKeyedArchiverを使用してアーカイブします。
基本的に、クラス「Brick」のいくつかのインスタンスの状態を保存しようとしているので、このようなgetBlueprintメソッドを実装しました(スリム化バージョン)
-(id)getBlueprint
{
// NOTE: brickColor is a string
NSDictionary *blueprint = [NSDictionary dictionaryWithObjectsAndKeys:
brickColor, @"color",
[NSNumber numberWithInt:rotation], @"rotation",
nil];
return blueprint;
}
そのため、ブループリントが提供されたときに新しいBrickインスタンスを作成する別のメソッドがあります。
-(id)initWithBlueprint:(NSDictionary *)blueprint spriteSheet:(NSString *)ssheet
{
if((self == [super init])){
brickColor = [blueprint objectForKey:@"color"];
[self setColorOffset:brickColor];
while(rotation != [[blueprint objectForKey:@"rotation"] intValue]){
[self setRotation:90];
}
}
return self;
}
これは、「新しい」ブループリントを渡すと機能しますが、保存されたファイルからブループリントを読み取ると機能しません...ある種。たとえば、回転は機能しますが、色を変更しても機能しません。だから私はbrickColorの値を使用して読むことができますが
NSLog(@"brick color %@", [blueprint objectForKey:@"color"]);
私が次のようなことをしようとすると
if(brickColor == @"purple"){
colorOffset = CGPointMake(72,36);
NSLog(@"Changed offset for -- %@ -- to %@", color, NSStringFromCGPoint(colorOffset));
}
そして、私は色が紫であることを知っています、条件は真に戻りません。どういうわけかNSKeyedUnarchiverが文字列を別のものに変更したのではないかと思いましたが、次のテストはtrueを返します。
if([color isKindOfClass:[NSString class]]){
NSLog(@"%@ IS A STRING", color);
}else{
NSLog(@"!!!!! COLOR IS A NOT STRING !!!!!");
}
私が言ったように、ブループリントがアーカイブされてから読み戻された場合にのみ、新しく作成されたNSDictionaryをブループリントとして使用しようとしても、これは問題ではありません。
だから、いつものように、なぜこれが起こっているのか誰かが何か考えを持っているのだろうかと思います。
関連する場合は、次のようにデータを保存および受信します。
// Saving
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-(void)buildLevelData{
levelData = [[NSMutableArray alloc] initWithCapacity:100];
for(brickSprite *brick in spriteHolder.children){
[levelData addObject:[brick getBlueprint]];
}
}
-(void)saveLevel
{
[self buildLevelData];
NSData *rawDat = [NSKeyedArchiver archivedDataWithRootObject:levelData];
if([self writeApplicationData:rawDat toFile:saveFileName]){
NSLog(@"Data Saved");
}else{
NSLog(@"ERROR SAVING LEVEL DATA!");
}
[[Director sharedDirector] replaceScene:[MainMenu scene]];
}
- (BOOL)writeApplicationData:(NSData *)data toFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(@"Documents directory not found!");
return NO;
}
NSString *appFile = [saveDir stringByAppendingPathComponent:fileName];
return ([data writeToFile:appFile atomically:YES]);
}
// Loading
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
- (void) loadRandomMapFrom:(NSString *)dir
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [paths objectAtIndex:0];
if(!docsDir){
NSLog(@"Cound Not Find Documents Directory When trying To Load Random Map");
return;
}
dir = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@", dir]];
// we'll also set the file name here.
NSArray *existingFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dir error:nil];
// get last file for this test
NSString *filePath = [dir stringByAppendingPathComponent:[existingFiles objectAtIndex:([existingFiles count] - 1)]];
NSMutableArray *levelData = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
[self buildMapWithData:levelData];
}
-(void)buildMapWithData:(NSMutableArray *)lData
{
for(NSDictionary *blueprint in lData){
brickSprite *brick = [[brickSprite alloc] initWithBlueprint:blueprint spriteSheet:@"blocks.png"];
[spriteHolder addChild:brick];
}
}
質問の混乱について申し訳ありません。自分自身を完全に理解するのに苦労していることがたくさんあるので、それを最小限に分解するのは難しいです。