2010-02-02 4 views
0

J'ai un fichier texte (songlist.txt) que mon application récupère d'un serveur et stocke localement. Lorsque j'essaie d'utiliser le contenu du fichier dans mon application, j'obtiens des résultats inattendus. Quand j'ai examiné le problème, j'ai trouvé que des caractères étranges apparaissaient parfois à la fin du contenu NSString du fichier texte.
Quels sont ces «personnages voyous» étranges à la fin de mon NSString?

Pourquoi ces caractères étranges sont-ils ici? (Ci-dessous est ma source):

Des exemples de caractères étranges à la fin de la chaîne:

1) I
2) upp †
3) mul †
4) ID

// exact Contenu du fichier texte (songlist.txt)

chanson One.mp3
chanson Two.mp3
Chanson Three.mp3
Chanson Four.mp3
Chanson Five.mp3

//Downloading Songlist from the internet, Saving it to Documents folder 
-(void)downloadSongList 
{ 
    NSString *songlistString; 

    NSString *URLString_list = @"http://et.bicdoss.com/app/songlist.txt"; 
    NSURL *URL_list = [NSURL URLWithString:[URLString_list stringByAddingPercentEscapesUsingEncoding:       NSASCIIStringEncoding]]; 

    // Get Data of Contents (for Write to Disk) 
    NSData *songData = [NSData dataWithContentsOfURL:URL_list]; 

    if(songData == nil) { 

     NSLog(@"SONGLIST.TXT NOT DOWNLOADED"); 

    }else{ 

    // Write Songlist.txt to disk 
    NSString *filePath = [[self documentsDirectory] stringByAppendingPathComponent:@"Music/songlist.txt"]; 
    [songData writeToFile:filePath atomically:YES]; 
     NSLog(@"SONGLIST.TXT DOWNLOADED"); 
    } 

} 

// Retrieve songlist.txt from local documents folder 
-(NSString*)getLocalSongList 
{ 
NSData *textFileData = [NSData dataWithContentsOfFile:[[self documentsDirectory] stringByAppendingPathComponent:@"Music/songlist.txt"]]; 
NSString *textFileString = [NSString stringWithUTF8String:[textFileData bytes]]; 

return textFileString;   
} 

//Create NSMutableArray of contents of songlist.txt (seperated by linebreaks) 

... 
NSMutableArray *files_catalog; 
    files_catalog = [[NSMutableArray alloc] init]; 
    [files_catalog addObjectsFromArray:[[self getLocalSongList] componentsSeparatedByString: @"\n"]]; 
    [files_catalog retain]; 

    for(int a = 0; a<[files_catalog count]; a++){ 
     NSLog(@"Contents of Object %i (files_catalog) %@", a, [files_catalog objectAtIndex:a]); 
/* 

NOTE: 
The above NSLog statement always gives strange output (note strange characters at the end of last filename:) 

(output) 
2010-02-02 21:22:27.145 SomeApp[19670:207] >>>>>>>>>>>>>>>>>>>> Cntnts of Objct 0 (files_catalog) Song One.mp3 
2010-02-02 21:22:27.145 SomeApp[19670:207] >>>>>>>>>>>>>>>>>>>> Cntnts of Objct 1 (files_catalog) Song Two.mp3 
2010-02-02 21:22:27.146 SomeApp[19670:207] >>>>>>>>>>>>>>>>>>>> Cntnts of Objct 2 (files_catalog) Song Three.mp3 
2010-02-02 21:22:27.146 SomeApp[19670:207] >>>>>>>>>>>>>>>>>>>> Cntnts of Objct 3 (files_catalog) Song Four.mp3 
2010-02-02 21:22:27.146 SomeApp[19670:207] >>>>>>>>>>>>>>>>>>>> Cntnts of Objct 4 (files_catalog) Song Five.mp3upp† 


Some other examples of strange characters at the end were: 

I 
upp† 
mul† 
ID 

*/ 
} 

... 

Répondre

2

Il semble que le codage se foiré lors de l'importation. Essayez d'utiliser la chaîne stringWithContentsOfFile: encoding: error: method.

[NSString stringWithContentsOfFile:[[self documentsDirectory] stringByAppendingPathComponent:@"Music/songlist.txt"] encoding:NSUTF8StringEncoding error:nil]; 

Plus il ignore l'étape NSData.

Questions connexes