2012-04-29 3 views
0

나는 환각 상태에 빠졌다고 생각합니다. 나는 Concentration-lke 게임에 끈기를 더하기 위해 노력 중이다. 나는 높은 점수를 추적하고 싶습니다. 나는이 부분적으로 오늘은 잠시 동안 일하고 있고, 이제는 모두 kablooie가되었다. (iOS 용어가 정확한 것 같다.) 자, 내 allHighScores NSMutablearray가 갑자기 CALayer가됩니다. NSKeyed Archiving을 사용하고 있습니다. allHighScores가 데이터로로드되기 전에 파일에 중단 점이 있습니다. 응용 프로그램을 단계별로 실행할 때 allHighScores가 NSMutableArray로 존재하며 다음 단계에서 갑자기 CA 계층이됩니다. 응?내 NSMutableArray가 갑자기 CALayer가됩니다.

-(id)init 
{ 
    self = [super init]; 
    if (self) { 
     NSString *path = [self flipScoreArchivePath]; 
     NSLog(@"Path is %@", path); 
     allHighScores = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; 
     if (!allHighScores) { 
     allHighScores = [[NSMutableArray alloc] init]; 
     } 
    } 
    return self; 
} 


+(FlipHighScoreStore *)sharedStore { 
    static FlipHighScoreStore *sharedStore = nil; 
    if (!sharedStore) { 
     sharedStore = [[super allocWithZone:nil]init]; 
    } 
    return sharedStore; 
} 

어떻게 든 호출 NSKeyedUnarchiver는있는 CALayer 내로있는 NSMutableArray에서 내 allHighScores을 변경합니다. 나 엄청 혼란스러워.

보관 처리 지침에 retain을 추가하려고 시도했지만 도움이되지 않았습니다.

-(void)encodeWithCoder:(NSCoder *)aCoder { 
[aCoder encodeObject:self.themeChosen forKey:@"themeChosen"]; 
[aCoder encodeInt:self.highScore forKey:@"highScore"]; 
[aCoder encodeInt:self.scoreStartLevel forKey:@"scoreStartLevel"]; 
[aCoder encodeInt:self.scoreFinishLevel forKey:@"scoreFinishLevel"]; 
[aCoder encodeObject:scoreDateCreated forKey:@"scoreDateCreated"];} 

-(id)initWithCoder:(NSCoder *)aDecoder { 
if (self) { 
    self.themeChosen = [aDecoder decodeObjectForKey:@"themeChosen"]; 
    self.highScore = [aDecoder decodeIntForKey:@"highScore"]; 
    self.scoreStartLevel = [aDecoder decodeIntForKey:@"scoreStartLevel"]; 
    self.scoreFinishLevel = [aDecoder decodeIntForKey:@"scoreFinishLevel"]; 
    scoreDateCreated = [aDecoder decodeObjectForKey:@"scoreDateCreated"]; 
} 
return self;} 

UPDATE :

가 여기 내 인코딩/디코딩 코드 프로그램이 충돌 "highscores.archive"파일이 이미 존재하고 저장이 다시 호출됩니다.

-(BOOL)saveHighScores { 
NSString *path = [self flipScoreArchivePath]; 
return [NSKeyedArchiver archiveRootObject:allHighScores toFile:path];} 

이 EXC_BAD_ACCESS 원인 : 그들이가 행복하게 검색하지만, 코드를 저장 - 나는 높은 점수를 보면, 응용 프로그램을 실행할 수 있습니다. 경로가 맞습니다. 어떻게 든 allHighScores는 그렇지 않습니다.

+0

ARC를 사용하고 있습니까? – joerick

+0

'FlipHighScore'에서 NSCoding 프로토콜을 구현하는 방법이나 클래스 이름이 무엇인지 보여줄 수 있습니까? – mbm29414

+0

이 NSLog (@ 보관되지 않은 클래스 % @ ", NSStringFromClass ([allHighScores class]));를 시도하고 출력을 기록하십시오. – CodaFi

답변

2

여기서 문제는 보관 취소 결과를 유지하지 않는다는 것입니다. Basic Memory Management Rules에 따르면 +unarchiveObjectWithFile:이라는 이름의 메서드는 자동 렌더링 된 개체를 반환합니다. 따라서, 당신이 ivar에 그것을 배치하고 있기 때문에, 당신은이 객체를 유지할 필요가 있습니다. 그렇지 않으면 그것은 당신 아래에서 할당 해제 될 것입니다. 당신이 변경할 수 배열을 원하기 때문에

는 귀하의 경우 비록, 당신은 실제로 당신에게 불변의 배열을 줄 것이다 -mutableCopy NSKeyedUnarchive 이후를 호출해야합니다.

-(id)init { 
    if ((self = [super init])) { 
     NSString *path = [self flipScoreArchivePath]; 
     NSLog(@"Path is %@", path); 
     allHighScores = [[NSKeyedUnarchiver unarchiveObjectWithFile:path] mutableCopy]; 
     if (!allHighScores) { 
      allHighScores = [[NSMutableArray alloc] init]; 
     } 
    } 
    return self; 
} 

귀하의 -initWithCoder: 슈퍼를 호출하지 않습니다. 당신은 말할 필요가

if ((self = [super initWithCoder:aDecoder])) { 
+0

고마워요! 나는 보유를 추가했고 행동에 변화가 없었다. 내가 ARC를 사용하고 있었기 때문에 나는 그 보지가 필요하다고 생각하지 않았다. –

+0

@ AugustusS-R : 아, 당신이 ARC를 사용하고 있다는 것을 몰랐습니다. 가변 배열을 원한다면 여전히'-mutableCopy'가 필요합니다. –

+0

Kevin에게 감사드립니다. allHighScores는 여전히 CALayer가됩니다 (분명히 나쁜 것입니다 ...) –

-3

당신은 이것을 시도 했습니까?

-(id)init { 
    if ((self = [super init])) { 
     NSString *path = [self flipScoreArchivePath]; 
     NSLog(@"Path is %@", path); 
     allHighScores = [[NSKeyedUnarchiver unarchiveObjectWithFile:path] mutableCopy]; 
     if (!allHighScores) { 
      allHighScores = [[NSMutableArray alloc] init]; 
     } 
     // All-important new line.... 
     [self setAllHighScores:allHighScores]; 
    } 
    return self; 
} 

편집/업데이트 : 그래서, 여기에 내가 실제로 (나는 그의 바르 allHighScores는 해당 속성을 가지고 여기에 있으리라 믿고있어) 위의 예에서 의도의 두 가지 버전입니다 :

:
-(id)init { 
    if ((self = [super init])) { 
     NSString *path = [self flipScoreArchivePath]; 
     NSLog(@"Path is %@", path); 
     self.allHighScores = [[NSKeyedUnarchiver unarchiveObjectWithFile:path] mutableCopy]; 
     if (!self.allHighScores) { 
      self.allHighScores = [[NSMutableArray alloc] init]; 
     } 
    } 
    return self; 
} 

이 내가 실제로 그것을 할 거라고 방법입니다

+2

최고의 카고 - 컬트 프로그래밍. ivar에 할당 한 다음 같은 setter를 호출합니까? 그건 말이 안돼. –

+0

return self; 실종됐다. – MrMage

+0

물론 그가 ARC를 사용하고 있기 때문에 객체가 단순히 ivar에 할당되어 유지되지 않습니다. 그는 이상한 문제가있다. 모든 기본 사항을 확인했는지 확인하고 싶었습니다. 도움에 정직한 시도를 투표 한 모두에게 감사드립니다 !!! 너 자신에 대해 더 기분이 좋니? – mbm29414