2014-05-25 8 views
-5

Je veux écrire dans un txt, mais mon code ne fonctionne pas ...iOS écrire à un txt

Ceci est mon code:

-(void)bestScore{ 
    if(cptScore > bestScore){ 
     bestScore = cptScore; 
     highScore.text =[[NSString alloc] initWithFormat: @" %.d", bestScore]; 
     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"bestScore" ofType:@"txt"]; 
     NSString *test = @"test"; 
     [test writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil]; 
    } 
} 

J'ai déjà un fichier nommé dans bestScore.txt mon dossier "Supporting Files"

Pouvez-vous m'aider s'il vous plaît?

Merci

EDIT: Je peux lire mon dossier "bestScore.txt" avec ce code:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"bestScore" ofType:@"txt"]; 
    NSString *textFromFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 
    highScore.text = textFromFile; 
    bestScore = [textFromFile intValue]; 
+0

Qu'est-ce qui ne fonctionne pas à ce sujet? Est-ce qu'il compile? Est-ce que ça plante? Est-ce que cela fonctionne, mais au lieu d'écrire "test", il écrit "TOUS HAIL STEVE JOBS SEIGNEUR DES IPHONES!"? – nhgrif

+0

Vous ne pouvez pas écrire dans un fichier à l'intérieur de votre ensemble d'applications dans iOS. Vous devriez écrire votre fichier dans ~/Library/Preferences ou ailleurs. –

+0

Merci pour les réponses. Ça ne marche pas car rien n'écrit dans mon txt, mais mon application fonctionne sans erreur! – user3657063

Répondre

0

Essayez ceci:

-(void)bestScore{ 
    if(cptScore > bestScore){ 
     bestScore = cptScore; 
     highScore.text =[[NSString alloc] initWithFormat: @" %.d", bestScore]; 
     NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences/bestScore.txt"]; 
     NSString *test = [NSString stringWithFormat:@"%@",bestStore]; 
     NSError *error; 

     // save a new file with the new best score into ~/Library/Preferences/bestScore.txt 
     if([test writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]) 
     { 
      // wrote to file successfully 
      NSLog(@"succesfully wrote file to %@", filePath); 
     } else { 
      // there was a problem 
      NSLog(@"could not write file to %@ because %@", filePath, [error localizedDescription]); 
     } 
    } 
} 

Et pour relire le score en , vous pouvez faire:

NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences/bestScore.txt"]; 
NSString *textFromFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 
if(textFromFile) 
{ 
    highScore.text = textFromFile; 
    bestScore = [textFromFile intValue]; 
} else { 
    // if the file doesn't exist or if there's an error, initialize bestScore to zero 
    bestScore = 0; 
    highScore.text = @""; 
} 
+0

Merci mais ça ne marche pas. Et ma console est vide:/ Mais si j'enlève le "!" dans le si, j'ai ce message: impossible d'écrire le fichier dans/Users/ludovic/Bibliothèque/Application Support/iPhone Simulator/7.1/Applications/10B15625-FFAF-4A11-AF35-1946CE978426/Bibliothèque/Preferences/bestScore.txt car (null) – user3657063

+0

J'ai changé mon code afin que vous puissiez voir qu'il a sauvé avec succès. "' writeToFile' "renvoie YES si le fichier a bien été enregistré et NO si le fichier a rencontré un problème d'écriture. –

+0

Quelle est la permission du fichier existant nommé bestScore.txt? Que se passe-t-il si vous supprimez ce fichier, l'opération d'écriture réussit-elle? –

Questions connexes