Iphone 这些奇怪的东西是什么;流氓角色';在我的字符串末尾?
我有一个文本文件(songlist.txt),我的应用程序从服务器检索并存储在本地。当我尝试在我的应用程序中使用文件的内容时,我得到了意想不到的结果。当我研究这个问题时,我发现文本文件的NSString内容末尾有时会出现奇怪的字符。Iphone 这些奇怪的东西是什么;流氓角色';在我的字符串末尾?,iphone,nsstring,Iphone,Nsstring,我有一个文本文件(songlist.txt),我的应用程序从服务器检索并存储在本地。当我尝试在我的应用程序中使用文件的内容时,我得到了意想不到的结果。当我研究这个问题时,我发现文本文件的NSString内容末尾有时会出现奇怪的字符。 为什么这些奇怪的人物在这里?(以下是我的资料来源): 字符串末尾的奇怪字符示例: 1) 我 2) upp† 3) mul† 4) ID //文本文件(songlist.txt)的确切内容 歌曲1.mp3 第二首歌。mp3 歌曲三。mp3 歌曲4.mp3 歌曲5.mp
为什么这些奇怪的人物在这里?(以下是我的资料来源):
字符串末尾的奇怪字符示例: 1) 我
2) upp†
3) mul†
4) ID
//文本文件(songlist.txt)的确切内容 歌曲1.mp3
第二首歌。mp3
歌曲三。mp3
歌曲4.mp3
歌曲5.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
*/
}
...
//从internet下载歌曲列表,并将其保存到Documents文件夹
-(作废)下载歌曲列表
{
NSString*songlistString;
NSString*URLString_list=@”http://et.bicdoss.com/app/songlist.txt";
NSURL*URL_list=[NSURL URLWithString:[URLString_list stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
//获取内容的数据(用于写入磁盘)
NSData*songData=[NSData dataWithContentsOfURL:URL_list];
if(songData==nil){
NSLog(@“SONGLIST.TXT未下载”);
}否则{
//将Songlist.txt写入磁盘
NSString*文件路径=[[self documents directory]stringByAppendingPathComponent:@“Music/songlist.txt”];
[songData WriteFile:文件路径原子:是];
NSLog(@“SONGLIST.TXT下载”);
}
}
//从本地文档文件夹检索songlist.txt
-(NSString*)getLocalSongList
{
NSData*textFileData=[NSData dataWithContentsOfFile:[[self documents directory]stringByAppendingPathComponent:@“Music/songlist.txt”];
NSString*textFileString=[NSString stringWithUTF8String:[textFileData bytes]];
返回textFileString;
}
//创建songlist.txt内容的NSMutableArray(以换行符分隔)
...
NSMUTABLEARRY*文件目录;
文件_catalog=[[NSMutableArray alloc]init];
[files_catalog addObjectsFromArray:[[self-getLocalSongList]组件由字符串分隔:@“\n”];
[文件目录保留];
对于(int a=0;a>>>>>>>>>>>>>>>>>>>>>>>>>>Objct 0(文件目录)Song One.mp3的CNNTs
2010-02-02 21:22:27.145 SomeApp[19670:207]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2010-02-02 21:22:27.146 SomeApp[19670:207]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2010-02-02 21:22:27.146 SomeApp[19670:207]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2010-02-02 21:22:27.146 SomeApp[19670:207]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Objct 4(文件目录)Song Five.mp3upp†
结尾处的其他一些奇怪字符示例如下:
我
upp†
mul†
身份证件
*/
}
...
导入时,编码似乎出错。请尝试使用stringWithContentsOfFile:encoding:error:method
[NSString stringWithContentsOfFile:[[self documentsDirectory] stringByAppendingPathComponent:@"Music/songlist.txt"] encoding:NSUTF8StringEncoding error:nil];
此外,它跳过了NSData步骤