0

일련의 NSDictionaries를 사용하여 일부 데이터를 저장하고 NSMutableArray에 저장하고 NSKeyedArchiver를 사용하여 보관합니다.NSKeyedArchiver를 사용하여 NSMutableArray에 저장된 NSDictionary에서 문자열 읽기 문제

나는 기본적으로 여러 인스턴스 클래스 '벽돌'의 상태를 저장하기 위해 노력하고있어, 그래서이 (몸매는 여전 하구나 버전) 같은 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; 

    } 

. 예를 들어 회전은 작동하지만 색상을 변경하지 마십시오. 내가

if(brickColor == @"purple"){ 
    colorOffset = CGPointMake(72,36); 
    NSLog(@"Changed offset for -- %@ -- to %@", color, NSStringFromCGPoint(colorOffset)); 
} 

뭔가를하려고하면 내가

NSLog(@"brick color %@", [blueprint objectForKey:@"color"]); 

를 사용 brickColor의 값을 읽을 수 있습니다 그리고 나는 색이 보라색 것을 알고 동안 그래서, 조건이 true 반환하지 않습니다. 어떻게 든 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]; 
} 

} 

죄송합니다. 저 자신이 완전히 이해하기 위해 고심하고 있다는 점이 너무나 많아서 최소한으로 줄이는 것은 어렵습니다.

답변

3

은 항상 포인터 동일성을 검사하기 때문에 문자열을 [firstString isEqualToString:secondString]과 항상 비교해야합니다. 두 문자열이 같은 위치에 저장되면 (동적으로 생성 된 객체와 문자열 상수를 비교할 때 절대로 존재하지 않을 것입니다).

+0

이렇게 간단합니다. 나는 지금 담배를 끊으려고 노력하고있다. 그리고 이것은 거의 나를 탭 위에서 뒤로 가게했다. 당신은 말 그대로 생명의 은인입니다. – gargantuan