2013-02-08 3 views
-2

나는 객관적인 C에 익숙하다. 문제는 내가 저장하려고하면 "scores.xml"이 덮어 쓰이지 않는다는 것이다. 내 프로젝트에 "scores.xml"을 가져 왔지만 그렇게하려고하면 덮어 쓰지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 누구든지 아이디어가 있습니까?objective c : 파일을 배열로 덮어 쓸 수 없다.

- (void) writeHighScores:(NSString *)PlayerName{ 
NSString *highscorePath = [[NSString alloc]init]; 
NSArray * paths = [[NSBundle mainBundle] pathsForResourcesOfType: @".xml" inDirectory:@""]; 
for (NSString * path in paths) 
{ 
    highscorePath = [path substringToIndex:([path length])]; 
} 

NSMutableArray *arrayFromFile = [NSMutableArray arrayWithContentsOfFile:highscorePath]; 

for (NSMutableArray *element in arrayFromFile){ 
    if (score > [[element objectAtIndex:1] intValue]){ 
     [element replaceObjectAtIndex:0 withObject:PlayerName]; 
     [element replaceObjectAtIndex:1 withObject:[NSString stringWithFormat:@"%d", score]]; 
     break; 
    } 
} 
NSLog(@"Highscore path = %@",highscorePath); 

    // Write array 
    [arrayFromFile writeToFile:highscorePath atomically:YES]; 
} 
+0

반환 값의 유효성을 검사하는 코드를 추가하십시오. 'if (! [arrayFromFile writeToFile : ...]) {NSLog (@ "writeToFile failed!"); }'. 이 실패의 이유 중 하나는 배열에 * 속성 목록 개체 *가 포함되지 않는다는 것입니다. 자세한 내용은 참조를 참조하십시오. – trojanfoe

+0

@ 트로이안 호 또는 OP를 생각하면 여기저기서 파일을 쓸 수는 없습니다. –

+0

하지만 다시, [이것은 많은, 많은 시간을 물었습니다 ...] (http://stackoverflow.com/questions/5853014/writing-into-a-file-objective-c) –

답변

3

1) 앱 번들에 쓸 수 없습니다. 기간. 다른 위치 (예 : Documents 디렉토리)에 쓰기.

2)이 코드는 어떻게해야하나요? 그것은 절대 의미가 없습니다.

NSString *highscorePath = [[NSString alloc]init]; 
NSArray * paths = [[NSBundle mainBundle] pathsForResourcesOfType: @".xml" inDirectory:@""]; 
for (NSString * path in paths) 
{ 
    highscorePath = [path substringToIndex:([path length])]; 
} 
관련 문제